Skip to content

Instantly share code, notes, and snippets.

@JodiWarren
Created October 26, 2019 17:12
Show Gist options
  • Save JodiWarren/c5d42f270efe026b48fedfae54c7f590 to your computer and use it in GitHub Desktop.
Save JodiWarren/c5d42f270efe026b48fedfae54c7f590 to your computer and use it in GitHub Desktop.
Filter SearchWP results by post type.
<?php
function filter_search_by_post_type( $included, $engine, $terms ) {
$post_type = get_query_var('post_type');
if (!is_array($post_type)) {
return $included;
}
$posts = new \WP_Query([
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'post_type' => $post_type,
'fields' => 'ids',
'posts_per_page' => 999
]);
$ids = $posts->posts;
return empty( $ids ) ? array( 0 ) : $ids;
}
add_filter( 'searchwp_include', __NAMESPACE__ . '\filter_search_by_post_type', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment