How to Limit WordPress Searches to Posts

I was finding that whenever I was using the search box on my site it would return posts as well as pages. I didn’t want users to find pages -only posts – so I found this simple fix.

Log into your site as an admin and go to the WordPress Dashboard of your site. Go to Appearance – Editor and open your theme’s functions.php file. At the bottom of the code add:

/* ———————————————————————— */
/* Limit WordPress search to posts only
/* ———————————————————————— */

function SearchFilter($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘post’);
}
return $query;
}

add_filter(‘pre_get_posts’,’SearchFilter’);

Now your search will only deliver posts based on your search parameters.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.