Skip to content

Snippet: Limit Search Results to Standard (Blog) Posts only

function myprefix_search_filter( $query ) {
	if ( ! is_admin() && $query->is_main_query() ) {
		if ( $query->is_search ) {
			$query->set('post_type', 'post');
		}
	}
}
add_action( 'pre_get_posts','myprefix_search_filter' );
All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top