You are currently viewing How to Make WordPress Search Results Page Shows WooCommerce Products Only

How to Make WordPress Search Results Page Shows WooCommerce Products Only

WordPress Search result page shows posts, pages, categories, and products. To make the search results page shows woocommerce products only you need to do the following.

  1. Add the code below to the functions.php file.
// Only show products in the front-end search results
add_filter('pre_get_posts','lw_search_filter_pages');
function lw_search_filter_pages($query) {
    // Frontend search only
    if ( ! is_admin() && $query->is_search() ) {
        $query->set('post_type', 'product');
        $query->set( 'wc_query', 'product_query' );
    }
    return $query;
}

2. The above code can also be added to the code snippet plugin. Download and install the code snippet plugin. Hover on the snippet Menu icon and click on Add New to add a new snippet. Now choose a php snippet and add the above code to it.

Kwabena Asare

Kwabena Asare is a Digital Marketer, Website designer, Graphic designer and a blogger. He is the Chief editor at Kaetech Digital.