Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How Display Search Results For Specific Post Types Only in WordPress

We do use Custom post types a LOT in our Wordpress projects. It’s the perfect way to fully customise the website for our clients and making the content entry as easy as possible for them.

For a business directory WordPress website with custom post types we’re developing at the moment, we only wanted to perform a site Search in the custom posts (the business directory). We didn’t want to display pages or regular blog posts and the search results page.

That’s very easy to do.

We simply modified the main query itself by using the pre_get_posts action.
In the functions.php file we added the following code:

function search_filter($query) {

if ($query->is_search && !is_admin() ) {
$query->set('post_type', 'businesses');
}

return $query;
}

add_filter('pre_get_posts', 'search_filter');

You can filter the search results by changing the values in the array variable. Right now it is set to display the custom post type ‘Businesses’.

The post How Display Search Results For Specific Post Types Only in WordPress appeared first on Good Websites.



This post first appeared on Blog - Good Websites, please read the originial post: here

Share the post

How Display Search Results For Specific Post Types Only in WordPress

×

Subscribe to Blog - Good Websites

Get updates delivered right to your inbox!

Thank you for your subscription

×