Search code examples
phpwordpresssearchwoocommercecategories

Get Search Count in WordPress WooCommerce Category Search


After my search query (with redirect to the main WP shop page ) my URL looks like

.../online-shop/?s=test&product_cat=0&post_type=product

I echo the count of the search results in the main WP shop template with the following code - which works fine

<?php echo $wp_query->found_posts; ?>

BUT unfortunately, the code doesn't work if a category is set to the search

.../online-shop/?s=test&product_cat=beauty&post_type=product

Any idea how to fix it?


Solution

  • After a lot of research i found a solution. The following works in both scenarios:

    <?php echo $GLOBALS['wp_query']->found_posts; ?>
    

    Hope it helps others too…