• Posted: 2012-04-26
  • Author: Pippin Williamson
  • Tags:

Include All Post Types in Search

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
<?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');
?>

Instructions:

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


Related links:
http://pippinsplugins.com
Share this snippet

If you like this snippet, share it with friends!