Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jchristopher/10b586aa52b6c3721f28 to your computer and use it in GitHub Desktop.
Save jchristopher/10b586aa52b6c3721f28 to your computer and use it in GitHub Desktop.
Integrate SearchWP with WP-Views Parametric Search
<?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