Last active
March 4, 2016 00:27
-
-
Save jchristopher/df91e4980b65ae0d5420 to your computer and use it in GitHub Desktop.
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 | |
function my_searchwp_include_only_category( $ids, $engine, $terms ) { | |
// if and only if a category ID was submitted: | |
// we only want to apply this limitation to the default search engine | |
// if you would like to change that you can do so here | |
if ( ! empty( $_GET['swp_category_limiter'] ) && 'default' == $engine ) { | |
$category_id = absint( $_GET['swp_category_limiter'] ); | |
$category_args = array( | |
'category' => $category_id, // limit to the chosen category ID | |
'fields' => 'ids', // we only want the IDs of these posts | |
'posts_per_page' => -1, // return ALL posts | |
); | |
$ids = get_posts( $category_args ); | |
// if there were no posts returned we need to force an empty result | |
if ( 0 == count( $ids ) ) { | |
$ids = array( 0 ); // this will force SearchWP to return zero results | |
} | |
} | |
// always return our $ids | |
return $ids; | |
} | |
add_filter( 'searchwp_include', 'my_searchwp_include_only_category', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment