Created
October 26, 2019 17:12
-
-
Save JodiWarren/c5d42f270efe026b48fedfae54c7f590 to your computer and use it in GitHub Desktop.
Filter SearchWP results by post type.
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 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