Family Best Time >> Electronics

How to exclude pages from WordPress search results

By default, the WordPress search feature displays published posts and published pages in search results. Often when users search for something on a blog, it's most likely a post rather than a page. In this article, we'll show you how to make your search more relevant and less concurrent by excluding pages from WordPress search results.

How to exclude pages from WordPress search results

First, add the following code to your theme's functions.php file or a site-specific plugin.

 // Excluir páginas de WordPress Search if (! Is_admin ()) function wpb_search_filter ($ query) if ($ query-> is_search) $ query-> set ('post_type', 'post'); devolver $ consulta; add_filter ('pre_get_posts', 'wpb_search_filter'); 

Explanation:This code first checks that the search is not originating from the WordPress admin pages. If not, it looks for posts by setting the post_type parameter.

You can also make it do the opposite by setting post_type to pages so that it only returns pages in search results.

We hope this article helped you learn how to exclude pages from WordPress search results. You may also want to see our list of the best WordPress search plugins to improve search on your site.

If you enjoyed this article, please subscribe to our WordPress YouTube Channel video tutorials. You can also find us on Twitter and Facebook.