I ran into an issue the other day where I had custom post types showing up in WordPress search results. If you are manually registering your post types simply add the ‘exclude_from_search’ argument before registering.

$labels = array(
	// ...
);

$args = array(
	'labels'  => $labels
	// ...
	'exclude_from_search' => true,
	// ...
);

register_post_type( 'my-custom-type', $args );

 

If a plugin is creating the custom post type, use the ‘init’ hook with a high priority to update those arguments after the plugin has registered the post type. To do this, add this to functions.php.

// functions.php

add_action( 'init', 'update_my_custom_type', 99 );

/**
 * update_my_custom_type
 *
 * @author  Joe Sexton <joe@webtipblog.com>
 */
function update_my_custom_type() {
	global $wp_post_types;

	if ( post_type_exists( 'my-custom-type' ) ) {

		// exclude from search results
		$wp_post_types['my-custom-type']->exclude_from_search = true;
	}
}

Your custom post type will no longer show up in the WordPress search results.

11 comments on “Exclude Custom Post Type From Search in WordPress

  1. Chillmen says:

    hi,
    Thank you for this code, i was searching for a solution to exclude woocommerce products from wp default search results and your solution is working perfectly.

    1. Braulio says:

      Hello Chillmen, where you hit the code to make it work ?, I have also WooCommerce and I keep searching, greetings.

  2. Bud says:

    Works perfectly! Thank you!
    For novices like me, two things to mention:
    Be sure to add your post type in both places in the code (I missed one the first time)
    Insert the code into the user/functions.php file (assuming that your theme has one) so it won’t be overwritten if you update the theme. Mine was at wp-content/themes/(theme name)/functions/user/functions.php

  3. Nikita says:

    Hello!
    thanks for sharing this code, but when i use it, i have some trouble:
    don’t showing products in product category page.
    i have no idea why?maybe your have some thoughts about it?
    Thanks.

  4. Nikita says:

    sorry for incorrect question(:
    i use woocommerce on my website, so i told about it.
    my code with custom_type=”product”.

    add_action( 'init', 'update_my_custom_type', 99 );
    function update_my_custom_type() {
    global $wp_post_types;

    if ( post_type_exists( 'product' ) ) {

    // exclude from search results
    $wp_post_types['product']->exclude_from_search = true;
    }
    }

  5. Ben says:

    This is cool… but when I use it to clean up products from my search, it nukes my product listings in the woo product pages too. I am guessing they use the same search functions… any idea how I might remove product results from just the wordpress search box? Thanks!

    1. HelpfulGuy says:

      You could always wrap the code in an is_search() conditional statement to ensure it only effects search pages. Food for thought.

  6. tracy says:

    This is exactly what I needed and was almost the solution…

    I realized when I click on my WooCommerce category, there are no products in it.

    Maybe the WooCommerce category link uses the WP search pull?

    Is there a way to modify the coding or can you suggest what I did wrong? ~ really appreciate the help

  7. bevin says:

    Worked just fine. Thank you.

  8. Gray Ayer says:

    I as well have been trying this for WooCommerce Products and couldn’t figure out the best way to wrap the function in a is_search() conditional. If anyone figures that out I’d love to see their solution.

  9. Jonas Lundman says:

    Using this method hides custom queries and leaves empty listings like in products or widgets.

    Set conditional instead is_search() and filter pre_get_posts

Comments are closed.