One of the many great benefits of using WordPress is that you are not just limited to pages and posts, you can create your own custom post types and display them on your website. However, in some situations, you may not want some of the custom post types you have created to appear in the WordPress search results. Fortunately, we can add a WordPress filter to specify which post types should be included in the WordPress search results.

The hook we want for this is pre_get_posts, which allows us to update the query before it is run. We can use this to set the post types we want the query to return.

The pre_get_posts hook is applied to every query WordPress runs, not just the search, so we need to add a conditional statement to control when our query change is applied. If we just want to apply this to the front end search, we will want to use an if statement to check if the query is a search query, and that it’s not being performed from the WordPress Admin Dashboard.

Here is an example of the final code that would limit the search results returned by WordPress to only pages, posts, and a custom “event” post type:

// Function to filter what post types should appear in the search results
function my_custom_search_filter( $query ) {

  // Check query is a search but not from the admin dashboard
  if ( !is_admin() && $query->is_search ) {

    // Set the post types to be included in the search results
    $query->set( 'post_type', array( 'post', 'page', 'event' ) );

// Return the query
return $query;

// Add the new search filter to the pre_get_posts hook
add_filter( 'pre_get_posts', 'my_custom_search_filter' );

With this added to the functions.php file, any searches performed by a visitor using the WordPress search would only return results from the page, post, and event post types. Any other post types would be ignored.