X

Include All Post Types in Search

Snippets by IsItWP

By default, WordPress search will only show your blog posts. You can also add the other post types in search to expand it.

Instructions: Add the following code to the functions.php file of your WordPress theme to include all post types in your search.


<?php
function pippin_include_post_types_in_search($query) {
	if(is_search() && is_main_query()) {
		$post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
		$searchable_types = array();
		if($post_types) {
			foreach( $post_types as $type) {
				$searchable_types[] = $type->name;
			}
		}
		$query->set('post_type', $searchable_types);
	}
	return $query;
}
add_action('pre_get_posts', 'pippin_include_post_types_in_search');
?>

You may also enjoy disabling public search in WordPress and filtering search by users in WordPress.

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!