WordPress Search for Custom Post Type
In this tutorial, we are going to create a search bar that will only search your custom post types in WordPress.
We will cover:
Let's dive in.
How to Create a Custom Post Type
Before we can create a search bar we need to create our custom type.
In this tutorial, we are going to create a “Book” custom type and then we will add some books. We will then create a search bar that only searches these books.
To start we need to create the “Book” custom post type and there are a few ways we can do that:
- You can add them by editing the
functions.php
file on your theme (not recommended but common) - You can create your custom plugin that adds the custom post type
- You can add them using the Custom Post Type UI
Options 1 and 2 above both use the same code to create a “Book” type. The last option uses a plugin.
All the options have the same result.
Let's look at how to do it using ways 1 and 2.
Adding a Custom Post Type with Code
The code below will create a new custom post type. We create a new function and then register it via the init
action on WordPress.
Once loaded the Custom Post Type will appear in the side menu.
Here is what the code would look like to add our “Book” custom post type:
function custom_book_post() {
$labels = array(
'name' => _x( 'Books', 'post type general name' ),
'singular_name' => _x( 'Book', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New Book' ),
'edit_item' => __( 'Edit Book' ),
'new_item' => __( 'New Book' ),
'all_items' => __( 'All Books' ),
'view_item' => __( 'View Book' ),
'search_items' => __( 'Search Books' ),
'not_found' => __( 'No books found' ),
'not_found_in_trash' => __( 'No books found in the Trash' ),
'parent_item_colon' => ’,
'menu_name' => 'Books'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our books and book specific data',
'public' => true,
'exclude_from_search' => false,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
);
register_post_type( 'book', $args );
}
add_action( 'init', 'custom_book_post' );
Often I see this code added to the end of the functions.php
file of your theme. This is a bad idea because when you change the theme you will lose your custom post type!
For this reason, it is better to take the code and create your WordPress plugin.
The code is the same for both techniques but I recommend that you create a plugin if you are going to use this method.
Often this is enough to put most people off. After all, editing PHP files is not for the faint-hearted.
That's why we can look at the plugin to do it for us.
Using a plugin to create a Custom Post Type
The easiest way to create a custom post type is to use the Custom Post Type UI. Once installed it allows you to create custom post types via the WordPress Admin area.
To create a new type we click on the “CPT UI” link on the menu and then “Add/Edit Post Types”.
This will open up a new form where you need to enter three bits of info. The first is the slug which is the word used in the URL.
For this example, I have used “books”.
This will make the URL look like this:
https://example.com/books/to-kill-a-mockingbird
You then need to enter the plural and singular so “Books” and “Book”. Once you have done this you can click the “Add Post Type” button.
Our custom post type is now created!
The plugin does the same thing as the code but you can achieve the same results with a few mouse clicks.
Next, we can add a book!
Creating a Custom Post
Now that we have created the book Custom Post Type let's create one.
You can see the Book custom post type in the menu. To create a new Book we follow the same process as you would with a new post.
Once we have given the book a title and some text we can click publish.
Now the custom post type is live and we can view it on the website.
If everything is set up you should now be able to use the search box on your site and your pages returned:
The search will return results for all post types. Including a mixture of posts, pages, and custom post types.
If you see your book in the results then jump to how to search only a custom post type.
If your custom type is not showing in the results then there is one more setting we can check. Let's look at this next.
My Custom Post Type is not Showing in Search
If your custom post type is not showing in the search results. And you have created a custom post type using code then we need to check this setting:
'exclude_from_search' => true,
If the above is set to true then it will stop the custom search type from showing in the search results. To fix this remove this line of code.
When using the Custom Post Type UI you can also set this setting.
To see if it is set to true you can edit the post type. You will then need to scroll down to the settings area and make sure that “Exclude from Search” is set to “False”.
If it is “True” then switch it to “False” and then scroll down to save the settings.
Next, let's create a search bar that only returns the books when we search.
How to Search only a Custom Post Type
As we have seen the default search will return results for all post types. To filter it only to show our books we need to change the search form.
I wish I could recommend a plugin for this but unfortunately, there is not a good one that exists. We are going to need to change the code!
Like earlier, there are two places that this code can exist. Either in the functions.php
file or create a custom plugin.
The quickest solution is to add it to the functions.php
file. Yet, if you do the search box will disappear when you change themes.
The code remains the same for both methods but I recommend that you create a plugin.
Here is the code:
function my_search_form( $form ) {
$form = '
<form id="search" action="' . home_url( '/' ) . '" method="get">
<input type="hidden" name="post_type" value="book" />
<input id="s" name="s" type="text" value="' . get_search_query() . '" />
</form>';
return $form;
}
add_filter( 'get_search_form', 'my_search_form' );
The change here is that we have added this line:
<input type="hidden" name="post_type" value="book" />
This sets the search to only by the custom post type “book”. Now when we do a search we will only see books in the results.
Congratulations, you now have a search bar that only returns the custom post type.
If anyone knows of a plugin that can do this then I would love to hear what it is and try it out.
WordPress Search for Custom Post Type, Final Thoughts
We have covered in this tutorial on how to create a WordPress search for custom post types. We have looked at three ways to create a custom post type:
- By adding code to the
functions.php
file - By creating a custom plugin to add the code
- By using the Custom Post Type UI plugin
We have learned that you can exclude the custom post type using exclude_from_search
.
We then created a new form for search adding a hidden post_type
field to filter the results to “book”.
Add this code as a plugin and not in functions.php
as you will lose it when changing themes.