To skip pages from WordPress search results, we use a filter function. By default, the WordPress search function will display published posts and published pages on the search results page. When users are looking for something in your blog, they are most likely to look for blog posts rather than pages. It makes sense to display only blog posts on the search results page.
In this article, we will show you how to make the search function on your WordPress site more relevant and less crowded by excluding pages from WordPress search results. You can also use the same function to exclude custom post types appearing in search results.
Just open your child’s theme functions.php file and add the function code listed below at the end of the page to skip pages from WordPress search results. You can also run this code through the code snippets plugin.
function SearchFilter($query) { if ($query->is_search) { $query->set('post_type', 'post'); } return $query; } add_filter('pre_get_posts','SearchFilter');
It uses the pre_get_posts hook to get search results. We have set the results to only post-type, you can also change the ‘post’ to ‘page’ to get the results for pages only in search results if you want. Read more articles about WordPress.