How to add Custom Post Type in WordPress Search Results

One of the common issues that most of the users face after creating a Custom Post Type in WordPress is that it does not reflect in your WordPress search results? Even if the user’s type the exact title of the article in the search bar, they still get the no result found message.  Are you facing a similar issue? Don’t worry, in this article, we will help you solve the problem.

Custom Post type mechanism is the perfect way to organize your website, especially if it focuses on more than one subject. You will have the power to divide your content based on the information that they contain.

Before we jump to the problem-solving part, let us first understand why does this issue occurs in the first place. This happens because by default Custom Post Types are not included in the WordPress search results function.  

In this article, we will show you how to solve this issue manually as well with the help of a third-party plugin.

Creating a function to include Custom Post Types in WordPress Search Results

In this method, we create “include_cpt_search” function which will use “pre_get_posts” filter. You can do lots of different things with the “pre_get_posts” filter, such as changing the number of posts a custom post type archive displays. Using this filter, WordPress will also include Custom Post Type in the search results. You need to make changes in the functions.php file of your theme folder. From your WordPress dashboard go to Appearance >> Theme Editor.

After entering the theme editor open function.php file.

In your function.php file copy and paste the following code.

/**
 * This function modifies the main WordPress query to include an array of 
 * post types instead of the default 'post' post type.
 */
<?php 
function include_cpt_search($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post_type', array( 'post', 'mobile', 'laptop' ) );
    }
  }
}

add_action('pre_get_posts','include_cpt_search');
?>

This function “include_cpt_search” filters our search results by adding new arguments to the query results. As you can see in the code, the following function will return content from each of these custom post types: post, mobile, and laptops.

You just need to change and/or delete the names of the custom post types in the array to match your custom post types that you want included in your WordPress search results.

Note: Whenever working in the theme editor, we recommend users to backup their website first in case any error occurs.

Ensuring your Custom Post Type show in WordPress search results for Custom Post Types UI Plugin Users

Custom Post Type UI provides an easy to use interface for registering and managing custom post types and taxonomies for your website.

This step is very similar to the previous one. In this step as well we will create a function “my_cptui_add_post_type_to_search” which will use “pre_get_posts” filter. Add the following code in your theme’s function.php file.

Note: Use this code if you want to add all CPTUI post types to your search results.

<?php
function my_cptui_add_post_type_to_search( $query ) {
	if ( is_admin() ) {
		return;
	}

	if ( $query->is_search() ) {
		$cptui_post_types = cptui_get_post_type_slugs();
		$query->set(
			'post_type',
			array_merge(
				array( 'post' ), // May also want to add the "page" post type.
				$cptui_post_types
			)
		);
	}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );

In this code we check to see if WordPress is performing a search. If that’s true, then we fetch an array of all CPTUI based post type slugs, and merge it into an array with the “post” post type. With that array constructed, we pass in it all into the query parameters so that WordPress knows to query within them all.

If you want include selected CPTUI post types to your search results then we recommend you to use the below mentioned code. This code is very much like the first one, except we’re only including specific post types from CPTUI.

<?php
function my_cptui_add_post_type_to_search( $query ) {
	if ( $query->is_search() ) {
		// Replace these slugs with the post types you want to include.
		$cptui_post_types = array( 'my_post_type', 'my_other_post_type' );

		$query->set(
			'post_type',
			array_merge(
				array( 'post' ),
				$cptui_post_types
			)
		);
	}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );

Using a plugin to include Custom Post Types in WordPress Search Results

If you don’t want to go through the trouble of manually entering the code then you can opt for this method. We use the “WP Extended Search” plugin for this method. With the help of this plugin, admin can modify WordPress default search to search in Post Meta, Categories, Tags, or Custom Taxonomies. Admin can select meta keys to search in, also can control the default behavior of search in the post title or post content. You can include or exclude post types to appear in search results.

You first need to install and activate the plugin.

On successful activation, go to Settings >> Extended Search page. On that page, you can select all the things that you want to show in your search results.

We hope this article helps your Custom Post Type search issue. Please drop your queries in the comment section.

Also, if you want us to cover a specific topic, you can drop that into the comment section as well.

Recent Articles

How to Add Tabs to WordPress Posts and Pages

Tabs effectively organizes the content and engages the user. You can divide your posts into tabs in order to save space and...

How To Check Whether Someone Blocked You On Instagram

All social media platforms including Instagram do not notify people when they get blocked by a user. But when someone’s post and...

Free Apps Make Money through These Innovative Ways

Have you always wondered why are some apps available to you free of cost? If you want to know how apps make...

How To Completely Remove Google Account From Chrome Browser

Google Chrome when used with your personal Google account lets you keep the data in sync with all your devices. Your YouTube...

How To Know If Your Android Smartphone Is Hacked And How To Fix It!

One of the worst nightmares you can ever witness is realizing that your Android Smartphone Is Hacked. Yes, you're allowed to be...

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here