Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To show a custom post type in your site’s front-end search results, make sure its registration does not exclude it from search, then add its registered type key to the front-end main search query. WordPress’s pre_get_posts hook is the documented way to adjust that query before it runs.

1. Check that the post type is eligible for search

Find the post type’s register_post_type() arguments and inspect exclude_from_search. WordPress defines true as excluding the type from front-end search and false as allowing it. If the argument is omitted, its default is the opposite of the value of public. See the register_post_type() reference.

Eligibility and query composition are separate concerns: a type that is not excluded can still be absent if the search query does not request it. If a plugin registers the type, use that plugin’s supported setting or filter where possible rather than registering the same type a second time. WordPress recommends placing custom post type functionality in a plugin so it can remain available when the theme changes; see Working with Custom Post Types.

2. Add the type to the front-end main search query

Add this code to the plugin or theme functionality that manages your site’s search. Replace event with the exact registered post type key, and adjust the array to include the content types you want search to return.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function mysite_include_event_search( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        $query->set( 'post_type', array( 'post', 'page', 'event' ) );
    }
}
add_action( 'pre_get_posts', 'mysite_include_event_search' );

The pre_get_posts action fires after query variables are created and before WordPress runs the query. Using the passed $query object’s is_main_query() and is_search() methods, with an admin check, confines the change to the intended front-end search request. WordPress cautions that global conditional functions may test the global query rather than the query instance being modified; see the hook reference.

Include every type that should remain in the results. For example, an array containing only 'event' narrows the search to that custom type; it will no longer request ordinary posts or pages. The example preserves both by specifying 'post', 'page', and 'event'.

3. Confirm the change and troubleshoot missing results

  1. Check the type key. Use the registered post type key—not its display name—in the post_type array.
  2. Check registration settings. Confirm exclude_from_search is not true, and review whether the type is publicly queryable.
  3. Check where the callback applies. Confirm the code runs on the front-end main search query, not only on an archive or secondary query.
  4. Check the full array. Make sure it contains all the existing content types you intend to keep searchable.
  5. Check other query modifications. If results are still missing, review theme code and plugins that also alter search arguments.
  6. Check entry visibility. Confirm the custom entries have a status and visibility appropriate for public search. The registration and query settings alone do not establish whether an individual entry should be publicly visible.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When the results come from a custom query

The code above targets the main search request. If a search interface runs its own query, set the appropriate WP_Query arguments for that query instead of changing the front-end main query. WordPress’s query_posts() reference recommends pre_get_posts for modifying the main query and WP_Query or get_posts() for general post queries.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.