Theme Functions, Wordpress

How to Include all Post Types in WordPress search results

0 44

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

  • 35+ Must Have Apps to Increase Productivity in Ubuntu

    Once you have started your way through linux, you might come across numerous applications which are being developed and released everyday. You might me interested in some and you might not.So, you arrive to an ultimate question “What are the apps that will increase my productivity?. Well, the answer is in this article which consists…

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

  • Maximizing Performance with useLayoutEffect React Hook

    Discover how to optimize your React apps with our comprehensive guide on ‘Maximizing Performance with useLayoutEffect React Hook’. Learn how to leverage this powerful hook for efficient DOM manipulation, eliminating visual glitches, and creating smoother transitions. Ideal for both novice and expert React developers.