X

Filter posts types from search results

I have worked on a few project where pages or a custom post type is not something that I want to show up in the search results. Adding this snippet to the functions.php of your WordPress theme will let you set what posts types are included when you search your WordPress site. $query->set('post_type', array('post','CUSTOMTYPE')); This sample will only search posts and a custom post type called CUSTOMTYPE you will of course want to change that to something appropriate but you get the idea.


Code Snippet:


function filter_post_type($query) {
if ($query->is_search) {
$query->set('post_type', array('post','CUSTOMTYPE'));
}
return $query;
}
add_filter('pre_get_posts','filter_post_type');

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!