Skip to content

Instantly share code, notes, and snippets.

@fieldoffice
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save fieldoffice/7e145bf2b8c37fc542a1 to your computer and use it in GitHub Desktop.

Select an option

Save fieldoffice/7e145bf2b8c37fc542a1 to your computer and use it in GitHub Desktop.
Wordpress - include custom posts in search results
function 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', 'include_post_types_in_search');
or
function searchAll( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'page', 'feed', 'nav_menu_item', 'custom_post_type1', 'custom_post_type2'));
}
return $query;
}
add_filter( 'the_search_query', 'searchAll' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment