After WordPress 3 update, everyone started using custom post types a lot. So here is a simple snippet which will automatically include all public post types in your search results. At the same time, This snippet will also remove the custom post types that have been excluded from searches.
Include Custom Post Types in WordPress Search Results
It’s very easy to implement this hack. Just need paste the following snippet in the functions.php file of your template.
<?php
function smashing_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', 'smashing_search');
?>
Breaking the Code
$post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
The above line is used to get all custom post types available in your blog. As you can see “Exclude_from_search” tag to be false, you can exclude certain post types which you want to hide from search results.
$searchable_types = array();
if($post_types) {
foreach( $post_types as $type) {
$searchable_types[] = $type->name;
}
}
If any post types are present, Then an array is created “searchable_types” and each post type is stored in the array.
Now those values are passed to the function as arguments to $query.
Suggestions
Hope you liked this trick. If someone has any suggestion of reducing the above code or improving its functionality then share it with us via comments. See you soon with another cool wordpress trick.
Related Posts
-
September 20, 2012
Popular
Editor’s Pick
-
December 15, 2012 Tips to shoot excellent Panoramic Photographs
Recently, making out panoramic photos has become much easier as your software stitches them quite beautifully together. Photoshop is quite skillful and master in doing that; it has got a command called Photo Merge that weaves your photos once you hit it. Now, software is mere medium which we use to ease our post works,…
-
December 15, 2010 35+ Inspiring Nightscape photography and Tips
Nightscape Photography is one of the eluding styles of photography in lines of selection of photography-style by a photographer. It is sometime possible to shoot Landscape panorama, sea views, and bridges at night in such a manner as if, it has been taken at day time. A cityscape at night with lights blazing away like…
-
January 14, 2010 25 Must have Portable apps for MAC OS X
Portable applications have changed the way of accessing software and other favorite applications. Now you can carry your favorite computer programs along with all of your bookmarks, settings, email and more with you. And you can use them on any Windows computer all without leaving any personal data behind. Portable applications provides the platform that…
-
February 19, 2010 200+ Ultimate Collection of Photoshop Plugins
Photoshop is a powerful image editing software which is very useful for web designers and photographers to create desired effects in the images. But sometimes it’s difficult to perform certain tasks manually in photoshop. Once there was a belief that only designers who have mastered photoshop can create special effects on photographs or images. But…
-
April 29, 2008 Hide Blogger Navigation Bar – CSS Hack
As we all know in blogger, above header there is navbar or navigation bar . Most of want to hide the navigation bar in blogger , but we don’t know how to do that. Never wonder here is a simple trick to hide the navigation bar in blogger . Follow these easy steps to paste…

