Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active March 31, 2021 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/7a4926bd6035fbf06689df2d7a2a72f3 to your computer and use it in GitHub Desktop.
Save jchristopher/7a4926bd6035fbf06689df2d7a2a72f3 to your computer and use it in GitHub Desktop.
Enfold theme live search integration with SearchWP (version 4 and version 3)
<?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