Pawan Kumawat

Search only posts (not pages) in WordPress

How To Limit Search Results For Specific Post Types in WordPress

 

Have you ever tried  to  limit your search results to specific post types? Its not very hard.

We can modify the search feature in WordPress by modifying the functions.php file.

 

This is very easy with using this default hook.

 

pre_get_posts

 

function searchfilter($query) {
 
    if ($query->is_search && !is_admin() ) {
        $query->set('post_type',array('post','page'));
    }
 
return $query;
}
 
add_filter('pre_get_posts','searchfilter');

 

See the line that says

 

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

 

By modifying this you can easily modify particular post type for search. simply add or remove particular post type in array variables.

 

Share This On :

Leave a Comment

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

Categories
Scroll to Top

Request A Quote