Choose Which Post Types Appear in Search
Sets exactly which content types the site search returns, for example posts, pages and your portfolio, instead of every public type.
Opens WPIDE on
Code
// Post types returned by the site search.
$wpide_search_post_types = array( 'post', 'page', 'portfolio' );
add_action( 'pre_get_posts', function ( $query ) use ( $wpide_search_post_types ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || $query->get( 'post_type' ) ) {
return;
}
$types = array_values( array_filter( $wpide_search_post_types, 'post_type_exists' ) );
if ( $types ) {
$query->set( 'post_type', $types );
}
} );
Notes
List the post type names to include. Types that do not exist are ignored.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.