search ]

Removing WooCommerce Products from WordPress Search Results

Here’s a scenario: On your E-Commerce website, you have products, blog posts, and pages. You want to limit search results either to products or to posts, depending on where the actual search is performed (for example, from which search widget the action is taken).

Unfortunately, the WordPress search widget will identify and display both products and posts in the search results page. This situation can confuse the customer when looking for a product but also receiving results of posts containing the searched string.

In this guide, we will learn how to use different widgets for product and post searches and see how to remove WooCommerce products from WordPress search results.

The Difference Between the WordPress Search Widget and WooCommerce’s

Both WordPress and WooCommerce come default with their own search widgets. These are separate widgets that function independently of each other. Here are the key differences:

  • WooCommerce’s search widget will return products that match the search criteria. In other words, only products from the store will appear in the search results.
  • WordPress’s search widget, on the other hand, will return results from all Custom Post Types, and products in the store are not excluded. This widget searches in all post types and displays results of posts, products, pages, etc., without any exceptions.

We, of course, want to achieve a situation where the default WordPress search widget doesn’t return any products in the search results when using this widget for such a search or another.

Separating Search Results by Content Type

The solution that allows identifying the content type in the search results is to create a function that hooks into the pre_get_posts function.

Since the WooCommerce search widget is already set to return only products from the store, what we need to change is the query performed by the default WordPress search widget itself.

In any case, add both widgets wherever you want on your site. It makes sense for the WooCommerce search widget to be displayed on the shop page and the default WordPress search widget on the blog page. They look like this:

WooCommerce and WordPress Search Widgets

After adding both widgets, add the following code to your theme’s functions.php file:

<?php
/**
 * Remove WooCommerce product and WordPress page results from the search form widget
 */
function sv_modify_search_query( $query ) {
    // Make sure this isn't the admin or is the main query
    if( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    // Make sure this isn't the WooCommerce product search form
    if( isset($_GET['post_type']) && ($_GET['post_type'] == 'product') ) {
        return;
    }

    if( $query->is_search() ) {
        $in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) );

        // The post types you're removing (example: 'product' and 'page')
        $post_types_to_remove = array( 'product', 'page' );

        foreach( $post_types_to_remove as $post_type_to_remove ) {
            if( is_array( $in_search_post_types ) && in_array( $post_type_to_remove, $in_search_post_types ) ) {
                unset( $in_search_post_types[ $post_type_to_remove ] );
            }
        }
        $query->set( 'post_type', $in_search_post_types );
    }

}
add_action( 'pre_get_posts', 'sv_modify_search_query' );

Note that you are not adding the opening PHP tag in the provided code!

You can control which Post Types will be removed from the default search results of WordPress in line number 20. In our case, we removed the product content type and the page content type.

Summary

If you don’t want to touch the code, the free plugin Search Exclude is available for you. It allows you to remove specific content types from the standard search results of WordPress. You can discover all registered content type names on your site using the get_post_types function.

It’s worth mentioning that you can upgrade the WordPress search mechanism using a wonderful plugin called Relevanssi, about which I wrote a separate post. Take a look… 🙂

Roee Yossef
Roee Yossef

I develop custom WordPress themes by design. I love typography, colors & everything between, and aim to provide high performance, seo optimized websites with a clean & semantic code.

0 Comments...

Leave a Comment

Add code using the buttons below. For example, to add PHP click the PHP button & add the code inside the shortcode. Typo? Please let us know...