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
-
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…
-
May 14, 2009 Read More hack for Blogger with automatic Thumbnail creator script
You might have noticed that since few weeks I have been using a strange script which enables us to summarize content and show only the summary with a thumbnail in Home Page , Archives page and Labels page. Since some of my readers requested this hack in gtalk , so I am going to explain…
-
April 24, 2023 The Battle Between CSS-in-JS and Traditional CSS: Pros, Cons, and Best Practices
If you’re a web developer, chances are you’ve heard of both CSS-in-JS and traditional CSS. Let’s start with a quick explanation of each: CSS-in-JS: A method of styling web pages where CSS styles are written in JavaScript files and applied to components at runtime. Traditional CSS: The standard way of styling web pages using a…
-
May 3, 2023 Mastering React Context API in 2023: Best Practices and Examples
React Context API is a feature in React that provides a way to share data between components without passing the props down manually at every level. It allows you to manage global states in your application and avoid prop drilling. Mastering React Context API in 2023 is essential for building scalable and efficient applications. It…
-
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…

