Paulund
2012-05-02 #wordpress

Include All Custom Post Types In Wordpress Search

Since Wordpress version 3.0 you can now create your own custom post types. Wordpress already comes with post types for things such as posts, pages, attachments, revisions and menus. Custom Post types allow you to create your own content item. These are commonly used for portfolio slides and news articles. The problem with custom post types is that they are not included in the Wordpress search functionality you can only see posts and pages.

Add the following to your functions.php file to include all post types in the search results.


<?php
function include_post_types_in_search($query) {
	if(is_search()) {
		$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', 'include_post_types_in_search');
?>

UpChecker

Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring