Last active
August 29, 2015 13:58
-
-
Save jchristopher/10211284 to your computer and use it in GitHub Desktop.
Use SearchWP in conjunction with Dave's WordPress Live Search. To utilize, add the following to your theme's functions.php and save yourself the trouble of editing the plugin files.
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 | |
function my_searchwp_dwls_alter_results( $search_results, $deprecated, $daves_live_search ) { | |
global $wp_query; | |
if( class_exists( 'SearchWP' ) ) { | |
// remove Dave's pre_get_posts | |
remove_action( 'pre_get_posts', array( 'DavesWordPressLiveSearchResults', 'pre_get_posts' ) ); | |
// set which search engine you'd like to use | |
$search_engine = 'default'; | |
// set up custom posts per page for Dave's Live Search | |
function my_searchwp_posts_per_page() { | |
return '10'; | |
} | |
add_filter( 'searchwp_posts_per_page', 'my_searchwp_posts_per_page' ); | |
// perform the search | |
$searchwp = SearchWP::instance(); | |
$query = isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : ''; | |
$search_results = $searchwp->search( $search_engine, $query, 1 ); | |
// Dave's uses the $wp_query global so we need to force our results | |
$wp_query->posts = $search_results; | |
$wp_query->post_count = $wp_query->found_posts = count( $search_results ); | |
} | |
return $search_results; | |
} | |
add_filter( 'dwls_alter_results', 'my_searchwp_dwls_alter_results', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment