Remove / exclude pages from search in wordpress

Sometimes we don’t want to show pages in the search results inside a wordpress website. The wordpress search is designed to bring all the matching results including pages. So when it contains pages such as privacy policy, terms and conditions etc it becomes less user friendly. How to remove pages appearing in wordpress website inner search results? To remove or exclude pages from search results one option is adding a function that returns only posts. Add code snippet shown below to functions.php file of your wordpress website.

This code first checks ( using ” $query->is_search ” ) if the query is a search operation. It then modifies query for searching only posts ( using ” $query->set(‘post_type’, ‘post’);  ” ) and query is executed.

This gives you only posts in the search results.

Enjoy WordPress!


Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: