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.
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.
Hello Chillmen, where you hit the code to make it work ?, I have also WooCommerce and I keep searching, greetings.
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
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.
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;
}
}
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!
You could always wrap the code in an is_search() conditional statement to ensure it only effects search pages. Food for thought.
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
Worked just fine. Thank you.
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.
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