Theme Functions, Wordpress

How to Include all Post Types in WordPress search results

0 24

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 Snippet

<?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
PHP Snippet

		$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.

PHP Snippet


		$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.

About the author / 

admin

Related Posts

Popular

Editor’s Pick

  • Blogger Search Form box code

    Recently I explained How to add Blogger search box code to your Blogger template. Now In this post I will explain How to customize the search box code to suit your Blogger template. In order to make it easier for you , I am going to explain this with two blogger search form example codes….

  • Optimization and acceleration of Windows : Disable Superfetch

    If you are a gamer or an advanced user that need’s a lot of memory on your windows 7  you can get it without having to add ram to your pc by just 2 or 3 click , Disable the superfetch If you are one of the people that use the ready boost just close…

  • 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…

  • 25+ Breathtaking examples of Natural sky Photography

    Many times throughout the day you look up to the sky and just see a blue or grey sky without much thought. But on some days, you look out into the distance and see something beautiful, something that captivates the beauty of the sky not seen on a daily basis. This collection of photography that…