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
-
August 21, 2012 50+ Amazing Olympics Infographics Inspirations
The London Olympics 2012 has proved to be a phenomenal attraction this year – attracting eyeballs and attentions like nothing else. Well, I caught on to the Olympics buzz quite late into the second week – watched it every day from 11 in the night (IST). However, I followed it regularly over the news papers…
-
December 28, 2009 70+ Stunning HDR images with useful Photoshop Tutorials
High Dynamic Range Imaging ( HDRI or just HDR ) is a technique which was developed to produce high dynamic range images from the photographs taken at different exposures of light. In comparison with normal digital imaging techniques HDR imaging techniques can produce a wider range of luminescence between dark and light. The main intention…
-
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…
-
June 10, 2012 Show Recent Posts With Thumbnail from Particular Label/Category in Blogger
Hey Blogger friends, Today i have come up with another cool blogger hack. It’s about showing recent posts with/without thumbnail from any particular label/category in blogger. As you can see in my sidebar, I have listed recent posts from each category, but i have done this in wordpress. So if you want to do same…
-
December 25, 2009 Page Navigation Hack for Blogger
Most said page navigation menu in the bottom of the page can appear only in WordPress blogs. But Now here is a blogger hack which will help you to show Page Navigation Menu in bottom of the page ( see screenshot ) . you can Add this Page Navigation hack to your blogger template either…

