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
-
July 31, 2008 Page Navigation Menu Widget for Blogger
Yesterday I wrote How to add Page Navigation Menu hack by editing your blogger template. Now today I would like to use a different method to add this Page Navigation Menu to our Blogger Blog. Most Bloggers will prefer this method to add Page navigation Menu , because this method is very easy than editing…
-
April 22, 2023 Mastering Webpack Module Federation: How to Solve Singleton Issues with Code Snippets.
Introduction Webpack Module Federation is a feature introduced in Webpack 5 that allows multiple projects to share code dynamically at runtime. This means that you can break up your application into smaller microfrontends and load them on demand. It’s important to solve singleton issues because when multiple instances of the same module are loaded, it…
-
April 23, 2023 Mastering the Art of Timelapse Photography: Tips and Techniques
Timelapse photography is a technique that involves capturing a series of photos over a period of time and then combining them into a video sequence to create the illusion of time passing quickly. Mastering this art can add a unique dimension to your photography skills. The importance of mastering timelapse photography lies in the fact…
-
December 2, 2010 45+ Surreal Infrared photography Inspirations and Tips
In Infrared Photography the image sensor used is highly sensitive to infrared wavelengths of light. We all know that white light is composed of several bands of colours known as spectrum and the wavelengths ranging between 750nm to about 1200 nm, falls on infrared (IR) band. The spectrum used is in this technique is termed…
-
October 29, 2012 9gag Clone Blogger Theme
Yo Fellas! Hope you are having a great time.I have been busy with server migration and web design works lately. At last i have got some time to write a quick post. In this post, I am looking to distribute my another Blogger template called “9Gag Clone”. Yes, as the name suggests its a 9gag.com…

