fbpx
How to Include All Public Post Types in WordPress Search

How to Include All Public Post Types in WordPress Search

This snippet will automatically include all public post types (that have not been excluded from searches), in your WordPress search results.

Instructions
Add this to your functions.php file

function wpsnippet_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', 'wpsnippet_include_post_types_in_search');

WordPress Hosting

Leave a Reply

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

WordPress Snippet Info

Tested with Wordpress v. 4.8
Last Modified: 06.03.2018
Last Reviewed: 06.03.2018

How to Include All Public Post Types in WordPress Search
4.1 (81.82%) 11 vote[s]
Please report broken snippets.
Report Broken Snippet