Custom Post Type

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.

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

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');
Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial