Croco Black Friday 2024 banner element
Copy discount code:
bfcroco2024
Days
Hours
Minutes
Croco Black Friday 2024 banner element
Help Center
WordPress Search for Custom Post Types
bulat
Alexander Bulat
WordPress Copywriter
Show all articles
Updated on
Useful Resources

WordPress Search for Custom Post Types

WordPress search functionality, by default, includes posts and pages in search results, excluding the Custom Post Types unless specifically configured. WordPress Custom Post Types allow the site owner to create diverse content types, such as portfolios, products, or events, without having to mix them up with pages. Ensuring these CPTs are searchable requires that the WordPress site owner make some adjustments.

We will look into two ways of making CPTs appear in WordPress search results: through plugins like JetSearch and by implementing some code into the functions.php file. Each of these methods has pros and cons, and which one is better for you depends on your experience and needs.

Table of Contents

Understanding WordPress Custom Post Types

WordPress Custom Post Type is a powerful feature that allows users to create custom content beyond the default posts and pages. This flexibility enables site owners to manage and display content in more meaningful ways, depending on their specific requirements.

Common use cases for CPTs:

  • portfolios to display a collection of work or projects;
  • products to showcase items in an online store;
  • events to list upcoming events with specific details;
  • testimonials to display customer feedback and reviews.

Despite their flexibility, by default, WordPress does not include CPTs in searches, which greatly limits the chances of discovering that content. To overcome this sort of limitation, one can make use of plugins or implement code snippets in themes’ functions.php to make CPTs appear in search results

Method 1: Enable CPT WordPress Search Using JetSearch Plugin

The use of a plugin to display CPTs in search results is the preferred method for many WordPress users, especially for those who are not comfortable with coding. Here are some key advantages:

  • Most of the time, plugins have a simple installation process and easy-to-navigate settings.
  • Most plugins boast user-friendly interfaces and, therefore, can be set up really quickly without requiring any technical knowledge.
  • With plugins, it’s not necessary to edit your theme files or write the code.

JetSearch is a lightweight, multi-functional search plugin designed by Crocoblock for Elementor, Gutenberg, and Bricks users. In a few clicks, it will help to include CPTs in search results and tune them up according to your vision with diverse settings and styles.

First of all, you need a CPT. You’ll actually require a plugin like JetEngine to create one; within this tool, you can create and edit as many CPTs as your project needs.

creating cpt with jetengine

If you don’t have the JetEngine yet, you can get it at the Crocoblock website or use any other plugin you’re comfortable with to create your CPTs. When you have enough CPTs created, it’s time to fine-tune the search. 

Let’s walk through a specific use case: I have a bookstore website with a dedicated page listing all authors. 

authors page on a bookstore

I want to add a search bar that allows users to search specifically for authors without adding posts or pages to the search results. This process can be completed in three steps.

Step 1: Install the JetSearch plugin

  1. Go to the Crocoblock.com website, create an account, and pay for the plugin membership.
  2. Download the JetSearch plugin from your Crocoblock account and install it on your website by going to the WordPress Dashboard > Plugins > Add New path.

Step 2: Add the AJAX Search widget to the page

On the page where all authors are listed, I’ll hover the “Edit with JetPlugins” to open the Elementor page builder.

editing authors page with elementor builder

In the Elementor sidebar, I need to find the AJAX Search widget.

finding ajax search widget in elementor editor

Next, I’ll drag the widget onto the page where I want the author search bar to appear.

adding ajax search widget to the page via elementor builder

Step 3: Customize the AJAX Search widget

Upon default, the AJAX Search widget has a number of settings that, in my case, are not necessary, like the Categories dropdown. Now, it’s time to configure the widget to include only authors CPTs.

The following settings are needed for my goal:

  • The Content tab, Search form section:
    • Show submit button — this switcher should be set to “No” to remove the Submit button;
    • Show categories — this switcher should be set to “No to remove the dropdown;
  • The Content tab, Search Query section:
    • Source — this option allows you to add specific search areas; if nothing is selected in the option, the search will be made over the entire site. Here, I’m going to choose the Writers and Book Authors CPT so that my store visitors will be able to search only for the authors using this search bar. 
fine-tuning ajax search widget

If necessary, you may customize other settings, such as search results layout, filters, and styling options. When you’re done with all customization, don’t forget to hit the “Publish” button.

Now, let’s go to the front end to see the result; if I type the last name of the author, the search will be performed only via the author’s custom posts, and the Wells H.G. page will instantly be found. 

search results

By following these steps, you can quickly set up a powerful search feature that includes CPTs, enhancing your site’s usability and content discoverability.

Method 2: Enable CPT WordPress Search by Modifying functions.php File

Alternatively, if you don’t want to use a plugin or perhaps you want finer control over how CPTs are included in search results, then you can modify your theme’s functions.php file.

NOTE

Please note that if you decide to alter the functions.php file of Theme A and then decide to change it to Theme B, you will need to transfer those changes to your new theme manually. This is why it’s recommended to create a child theme and make all the necessary changes there.

Here are a few advantages of this option.

  • In regard to performing searches, there are no limitations other than what a particular plugin can handle. You can customize the search functionality to your liking.
  • It ensures less risk of compatibility or updating problems, which is dependent on an external third-party developer.
  • This method can be convenient for those who need certain special search behaviors or have some very specific demands.

Step 1: Creating a new custom post type

Before including CPTs in your WordPress website search, you need to create them. To create a new CPT, you can add the following code to your theme’s functions.php file:

  1. Go to WordPress Dashboard > Appearance > Theme Editor (alternatively, you can find this file using your hosting file manager app or FTP client via the path: wp-content/themes/your-active-theme/functions.php).
  2. Open the functions.php file of your active theme.
  3. Copy and paste this code snippet to create a new CPT.

NOTE

If, for some reason, the Theme Editor is not available in your WordPress Dashboard, you can try the following steps to enable/access it. 1) Open the wpconfig.php file and find the line define( ‘DISALLOW_FILE_EDIT’, true ); and change true to false. 2) Alternatively, you can try to go to yourdomain.com/wp-admin/theme-editor.php to access the file editor manually.

function create_custom_post_type() {
    $labels = array(
        'name'               => _x('Books', 'post type general name'),
        'singular_name'      => _x('Book', 'post type singular name'),
        'menu_name'          => _x('Books', 'admin menu'),
        'name_admin_bar'     => _x('Book', 'add new on admin bar'),
        'add_new'            => _x('Add New', 'book'),
        'add_new_item'       => __('Add New Book'),
        'new_item'           => __('New Book'),
        'edit_item'          => __('Edit Book'),
        'view_item'          => __('View Book'),
        'all_items'          => __('All Books'),
        'search_items'       => __('Search Books'),
        'parent_item_colon'  => __('Parent Books:'),
        'not_found'          => __('No books found.'),
        'not_found_in_trash' => __('No books found in Trash.')
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array('slug' => 'book'),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
        'show_in_rest'       => true,  // if set to true it enables Gutenberg editor support for the CPT
        'taxonomies'         => array('category', 'post_tag'),  // this enables categories and tags for your CPTs
    );


    register_post_type('book', $args);
}
add_action('init', 'create_custom_post_type');
adding cpt books via functions.php file

In the same way, I can add another CPT for book authors.

adding cpt book author via functions.php file

Don’t forget to click on the “Update File” to save changes.

The code I’m using creates a new CPT called “Books” or “Book Authors”. You can customize the labels and arguments to create any other CPT. The show_in_rest parameter is set to true to enable Gutenberg editor support for the CPT:

'show_in_rest'       => true,

If you want to add categories and tags to your CPTs, you can taxonomies parameter to the snippet:

'taxonomies'         => array('category', 'post_tag'),    

After editing the functions.php file, I have custom posts Books and Book Authors (with categories and tags enabled) in my WordPress dashboard.

custom posts Books and Book Authors WordPress dashboard

Now, it’s time to make these CPTs searchable by the website search.

When we have our CPTs all set, we need to modify the search query to include them; in my case, I want the search only to find CPTs from the book type. Go back to where you were editing the functions.php file, and paste the following code there:

function search_only_books($query) {
    if ($query->is_search && !is_admin()) {
        // Modify the search query to only include the 'book' CPT
        $query->set('post_type', 'book');
    }
    return $query;
}
add_filter('pre_get_posts', 'search_only_books');

You can add multiple CPT slugs if needed. Save changes when you’re ready.

adding cpt to search via functions.php

Now, let’s go to the front end to see the result; if I type the last name of the author, the search will be performed only via the book custom posts, and the novel by Wells H.G. will instantly be found.

cpt search results page

At the same time, I have several blog posts that contain the same search query, and as you can see, they were not added to the search results.

book store blog posts

Which Method to Choose?

Choosing between using a plugin or modifying the functions.php file depends on several factors, including your skill level and specific requirements.

When to use plugins

  • Plugins are user-friendly and don’t require coding knowledge, making them suitable for beginners or those uncomfortable with code.
  • If you need a quick solution with minimal effort, plugins are the way to go.
  • Many plugins offer additional features like search filters, custom widgets, and better search algorithms.

When to opt for code customization

  • If you have specific requirements that plugins cannot meet, modifying the functions.php file gives you complete control over the search functionality.
  • This method is ideal for developers or advanced users who are comfortable with coding and want to optimize their site without relying on third-party plugins.
  • Custom code can be more efficient and have a smaller impact on performance compared to plugins.

FAQ

How do I create a WordPress Custom Post Type without coding?

If you’re not comfortable with coding, you can create a Custom Post Type (CPT) using a plugin like JetEngine. This plugin provides an easy-to-use interface for creating and managing CPTs without needing to edit any code. After installing and activating the plugin, you can navigate to WordPress Dashboard > JetEngine > Post Types to set up your new CPT. You can specify labels, settings, and other options directly from the plugin’s settings page.

Will including Custom Post Types in search results affect my site’s performance?

Including Custom Post Types in search results generally has a minimal impact on your site’s performance, especially if you are using a well-optimized theme and have a good hosting environment. However, the performance impact can vary depending on how many CPTs and posts you have, as well as how complex your search queries are. To minimize performance issues, make sure to optimize your database and use caching plugins where appropriate. If you’re using plugins for search functionality, choose ones known for their performance and efficiency.

What are the main differences between a Custom Post Type and a standard post or page in WordPress?

Custom Post Types differ from standard posts and pages in several ways. The primary difference is that CPTs allow you to create new content types that cater specifically to your site’s needs, such as portfolios, testimonials, or products. Unlike standard posts, which are typically organized chronologically and are part of the blog, CPTs can be structured and displayed differently. For example, they can have unique custom fields, taxonomies, templates, and archive pages. This flexibility allows CPTs to be tailored for different types of content that don’t fit the standard blog format, providing a more organized and specific presentation for other content types on your site.

Conclusion

Including CPTs in your WordPress search results can significantly enhance your site’s functionality and improve user experience. Whether you choose to use a plugin or modify the functions.php file, both methods offer distinct advantages. JetSearch provides an easy, feature-rich solution for beginners and those looking for quick results. 

In contrast, custom coding provides more control and customization for developers and advanced users. Consider your needs and skills when deciding which method to use, and enjoy a more versatile search experience on your WordPress site.

jetsearch banner image
Build custom search for CPT using Crocoblock
Try JetSearch