Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active March 8, 2019 15:03
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/7491200 to your computer and use it in GitHub Desktop.
Save jchristopher/7491200 to your computer and use it in GitHub Desktop.
Limit SearchWP results to a single category
<?php
function searchwp_include_only_category( $ids, $engine, $terms ) {
// Limit to category 10
$categoryID = 10;
// Retrieve the IDs of all the posts in this category
$args = array(
'category' => $categoryID,
'fields' => 'ids',
'nopaging' => true,
);
$ids = get_posts( $args );
return empty( $ids ) ? array( 0 ) : $ids;
}
add_filter( 'searchwp_include', 'searchwp_include_only_category', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment