Theme Functions, Wordpress

How to Include all Post Types in WordPress search results

0 41

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+ Inspiring Nightscape photography and Tips

    Nightscape Photography is one of the eluding styles of photography in lines of selection of photography-style by a photographer. It is sometime possible to shoot Landscape panorama, sea views, and bridges at night in such a manner as if, it has been taken at day time. A cityscape at night with lights blazing away like…

  • 10 Expert Newborn Bottle Feeding Tips for Happy and Healthy Babies

    Introducing bottle feeding to your newborn is a crucial step in ensuring their health and well-being. As parents, it’s important to be aware of the best practices for bottle feeding to ensure that your baby is happy and healthy. Here are 10 expert tips to help guide you through the process. Importance of Bottle Feeding…

  • Responsive Web design Tips : #1 SCALING IMAGES

    Responsive Web Design is an approach in which a designer intends to provide an optimal viewing experience. Before knowing about the concept of scaling images, let’s have a look on “Responsive Web Design”. Nowadays people want to get access to the Internet data from any place using a wide range of portable devices- desktops, laptops,…

  • Ultimate List of Portable Applications for USB Stick

    Create Portable Applications CEEDO Portable Application creator Ceedo is something similar to Mojopac and will let you port your operating system to your USB drive. Ceedo uses virtualisation technology and algorithms to acheive this. Ceedo is also not free and you need license to use both Ceedo Personal and Ceedo Enterprise. Download Here   Prayaya…

  • 40+ best online coloring tools for graphic designers

    If you are a web designer or dealing with some graphic design project then you will need some coloring tools for getting color hints from ready made color templates from professional designers. There are lots of Online color tools, but it will take time to search them all via Google. So i have tried to…