Adding Custom Post Types To Your Search Query

Jan 06

All you need to do is create a function, which can be add to your functions.php file. This function checks if the $query variable is the search query $query->is_searchpasses the $query and then sets the post types which should be searched using the queried keyword you entered in the form, then returns what the $query should search within.

// Define what post types to search
function searchAll( $query ) {
	if ( $query->is_search ) {
		$query->set( 'post_type',
	array('post','page','feed','custom_post_type1',
'custom_post_type2', 'custom_post_type3',
'custom_post_type4' ));
	}
	return $query;
	}
// The hook needed to search ALL content
	add_filter( 'the_search_query', 'searchAll' );

Related Posts:

  • No Related Posts

Leave a Reply