Limiting Search Results For Certain Post Types In WordPress

It is easy to limit search results for specific post types in WordPress blog. This can be done by editing your theme’s PHP function.
Open WordPress theme’s editor from Dashboard >> Themes >> Editor and select the theme you are using. Select your theme’s function.php file and add the following code:

function searchfilter($query) {

if ($query->is_search)

{

$query->set(‘post_type’,array(‘post’,’page’));

}

return $query;

}

add_filter(‘pre_get_posts’,’searchfilter’);

Filtering of search results can be done by changing the values in array variable (in bold text). In the above given code post and pages are set, but you may edit it to display anything you like.

Leave a Reply

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