How to Include Custom Post Types in WordPress Search Results

What is Custom post types

Custom post types created using plugins like Advanced Custom Fields (ACF) are often not included in the default WordPress search results. This can be a significant issue, as custom post types are a powerful feature that allow you to extend the functionality of your WordPress site beyond the standard “post” and “page” post types.

When users perform a search on your WordPress site, the default search engine will only return results from the standard post and page content. Any content stored in your custom post types will be effectively hidden from the search, making it difficult for users to discover and access that information.This lack of searchability for custom post types can be a major roadblock, especially for sites with a large amount of specialized content organized into custom post types.

Without the ability to search across all of your site’s content, users may struggle to find the information they need, leading to a poor user experience and potentially impacting your site’s overall effectiveness.

In the following sections, I will show you how to ensure your custom post types created with ACF are included in the WordPress search results, making all of your site’s content easily discoverable and accessible to your users.

custom post type

Solving the Problem

Modifying the Main WordPress Query to Include Custom Post Types

Before making any modifications to your WordPress site’s functionality, it’s important to ensure you’re working within a child theme. This will prevent your customizations from being overwritten during future theme updates. If you haven’t already, go ahead and create a child theme for your current WordPress theme.

Once you have your child theme set up, you can start adding the necessary code to your functions.php file to include your custom post types in the WordPress search results.

/** Add custom post type created by ACF to search result **/
function tg_include_custom_post_types_in_search_results( $query ) {
    if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
        $query->set( 'post_type', array( 'post', 'movies', 'products', 'portfolio', 'software', 'video' ) );
    }
}
add_action( 'pre_get_posts', 'tg_include_custom_post_types_in_search_results' );

function tg_include_custom_post_types_in_category_archives( $query ) {
    if ( $query->is_main_query() && $query->is_category() && ! is_admin() ) {
        $query->set( 'post_type', array( 'post', 'movies', 'products', 'portfolio', 'software', 'video' ) );
    }
}
add_action( 'pre_get_posts', 'tg_include_custom_post_types_in_category_archives' );

Let’s break down the code:

  1. The tg_include_custom_post_types_in_search_results() function is hooked to the pre_get_posts action, which allows us to modify the main WordPress query before it’s executed.
  2. Inside the function, we check if the current query is the main query, is a search query, and is not being executed in the admin area.
  3. We then use the $query->set() method to set the post_type parameter to an array containing the default 'post' post type, as well as the custom post types you’ve created, such as 'movies''products''portfolio''software', and 'video'.

By adding your custom post types to the array, the search results will now include content from those post types, in addition to the default 'post' post type.

Make sure to replace the custom post type names in the example ('movies''products', etc.) with the actual names of your custom post types. This code should ensure that your custom post types are included in the WordPress search results.

Conclusion

In this article, we’ve explored the issue of custom post types created using plugins like ACF not being included in the default WordPress search results. This can be a significant problem, as it makes it difficult for users to discover and access all the content on your site.

To solve this problem, we covered the key steps:

  1. Modify the Main WordPress Query: By hooking into the pre_get_posts action and updating the post_type parameter in the main query, you can ensure your custom post types are included in the search results.
  2. Ensure Searchability of Custom Post Types: When registering your custom post types, make sure to set the exclude_from_search parameter to false to make them searchable.
  3. (Optional) Use a Search Plugin: For more advanced search functionality and customization, consider using a plugin like SearchWP, which can provide even greater control over including custom post types in the search results.

By implementing these solutions, you’ll be able to make all of your site’s content, including that stored in custom post types, easily discoverable and accessible to your users. This not only improves the overall user experience but also enhances the content discoverability and searchability of your WordPress site.I encourage you to take the time to implement these solutions in your own WordPress projects. Empowering your users to find the information they need, regardless of how it’s structured on your site, is a crucial step in creating a truly effective and user-friendly WordPress experience.

Scroll to Top