Last active
August 29, 2015 14:03
-
-
Save jchristopher/10b586aa52b6c3721f28 to your computer and use it in GitHub Desktop.
Integrate SearchWP with WP-Views Parametric Search
This file contains 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 | |
/** | |
* Tell WP Views Parametric Search to restrict it's results pool | |
* to what SearchWP finds, based on our custom setup | |
*/ | |
// tell WP-Views to limit the results pool | |
function my_searchwp_wpvparametric( $query ){ | |
global $searchwp; | |
// UPDATE THESE | |
$search_query_variable_name = 'wpv_post_search'; | |
$search_paged_variable_name = 'wpv_paged_preload_reach'; | |
$searchwp_engine_name = 'default'; | |
// retrieve our search query if applicable | |
$search_query = isset( $_REQUEST[ $search_query_variable_name ] ) ? sanitize_text_field( $_REQUEST[ $search_query_variable_name ] ) : ''; | |
// retrieve our pagination if applicable | |
$swppg = isset( $_REQUEST[ $search_paged_variable_name ] ) ? absint( $_REQUEST[ $search_paged_variable_name ] ) : 1; | |
if( ! empty( $search_query ) && class_exists( 'SearchWP' ) ) { | |
add_filter( 'searchwp_posts_per_page', 'my_searchwp_wpv_ppp' ); | |
add_filter( 'searchwp_load_posts', '__return_false' ); | |
$posts = $searchwp->search( $searchwp_engine_name, $search_query, $swppg ); | |
if ( empty( $posts ) ) { | |
$posts = array( 0 ); | |
} | |
$query['post__in'] = $posts; | |
} | |
return $query; | |
} | |
add_filter( 'wpv_filter_query', 'my_searchwp_wpvparametric' ); | |
// callback to set up custom posts per page | |
function my_searchwp_wpv_ppp() { | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment