Skip to content

Instantly share code, notes, and snippets.

@pippinsplugins
Created April 21, 2012 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pippinsplugins/2438344 to your computer and use it in GitHub Desktop.
Save pippinsplugins/2438344 to your computer and use it in GitHub Desktop.
All Post Types in Search
<?php
function ecpt_include_post_types_in_search($query) {
if(is_search()) {
$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', 'ecpt_include_post_types_in_search');
@helen
Copy link

helen commented Apr 21, 2012

You should probably be checking $query->is_main_query().

@pippinsplugins
Copy link
Author

Good call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment