Last active
March 31, 2021 22:38
-
-
Save jchristopher/7a4926bd6035fbf06689df2d7a2a72f3 to your computer and use it in GitHub Desktop.
Enfold theme live search integration with SearchWP (version 4 and version 3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// **********************************************************************// | |
// Enfold Search WP integration */ | |
// **********************************************************************// | |
add_filter( 'avf_ajax_search_function', 'avia_init_searchwp', 10, 4 ); | |
function avia_init_searchwp( $function_name, $search_query, $search_parameters, $defaults ) { | |
$function_name = class_exists( 'SWP_Query' ) ? 'avia_searchwp_search' : $function_name; | |
return $function_name; | |
} | |
function avia_searchwp_search( $search_query, $search_parameters, $defaults ) { | |
$searchwp_query = new SWP_Query( array( | |
'engine' => 'default', | |
's' => isset( $search_parameters['s'] ) ? sanitize_text_field( urldecode( $search_parameters['s'] ) ) : '', | |
'posts_per_page' => 5, | |
'fields' => 'ids', | |
) ); | |
if ( ! empty( $searchwp_query->posts ) ) { | |
foreach ( $searchwp_query->posts as $key => $result ) { | |
$searchwp_query->posts[ $key ] = get_post( $result ); | |
$searchwp_query->posts[ $key ]->post_excerpt = get_the_excerpt( $result ); | |
} | |
} | |
return $searchwp_query->posts; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment