WooCommerce product search not showing results

We have been working on a client’s WooCommerce store and for some unknown reasons the Product Search wasn’t working correctly.  I have spent multiple days debugging this problem and will post the steps I have made to finally track down this problem and fix it.

The Problem.

Our client’s website has the plugin “Advanced Woo Search” installed, which is working correctly.  However, in the search box, if a user clicks Enter, it will go to the default search page and most of the time there will be no results, even though the Advanced Woo Search displays some products.  We have tested many different query string and most of the expected results are not showing.

The Solution.

Below are the steps we have taken to debug this issue and find the reason why the correct results weren’t being returned.

  1. The first thing we checked is to see if this is a plugin conflict.  We have found two plugins which could possibly cause a problem with the standard search.  These are “Advanced Woo Search” and “WP Super Cache”.  So we deactivated these plugins and tested the search again.  The problem persisted.
  2.  Next we checked the Catalogue visibility of the products to make sure Catalogue/Search is selected.
    woocommerce catalogue search
    Catalogue/Search were selected for all our products, and searching for a string from the product title return an empty result.
  3. After step 2, we realised we had to get our hands dirty and do some serious debugging.  To do this, we install the plugin “Debug Bar”, which will display a debug menu to the admin menu.  This will provide us useful information to help track down the culprit.  After installing the plugin, the following line was added to wp-config.php.
    define(‘SAVEQUERIES’, true);
    Next we do the search again, then we click on the “Debug” button in the admin bar.
    Plugin Debug Bar
    Then we check out WP Query tab on the left hand side.
    Debug WP QueryIn the Query SQL section, the following SQL was return:SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts
    LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1
    AND (wp_term_relationships.term_taxonomy_id IN (102,193))
    AND (((wp_posts.post_title LIKE ‘%speedo%’) OR (post_excerpt LIKE ‘%speedo%’) OR (wp_posts.post_excerpt LIKE ‘%speedo%’) OR (wp_posts.post_content LIKE ‘%speedo%’)))
    AND (( wp_postmeta.meta_key = ‘_visibility’ AND wp_postmeta.meta_value IN (‘visible’,’search’) ))
    AND wp_posts.post_type = ‘product’
    AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘private’)
    GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC LIMIT 0, 20
    We tested the above query in phpMyAdmin and 0 results were returned.After analysing the query, we removed the line “AND ( wp_term_relationships.term_taxonomy_id IN (102,193)) ” and tested it in phpMyAdmin and sure enough the search returned some products.  Now we have to figure out which plugin/template added that in the search query.
  4. We suspected that it may be our theme that may be causing this, so we have changed the theme and see if it returns any results.  And yes it did, so this confirmed our suspicion.
  5. Next we added the following code to function.php in the theme folder:
    //WooCommerce Search Query
    function woo_search_query( $query ) {
    if ( $_GET[‘s’] && $query->is_main_query() ) {
    print_r($query); exit;
    }// Hook the above function to the pre_get_posts action
    add_action( ‘pre_get_posts’, ‘woo_search_query’ );
  6. The above code will display an array which is used by WordPress to construct the search query.  We have discovered that ‘tax_query’ limited the product search to search for products in two categories only.  So we search the the string ‘tax_query’ from all files in the theme folder, and have discovered these codes were added from some theme options.
  7. Knowing that it was some theme option which caused this, we went into WordPress administration and went to the theme options.  This will be different depending on the theme used.  For this website, it was in Appearance -> WooCommerce.  We found an option where users can limit the products display on /shop by categories.  Someone who worked on this website has limited it to two categories.  It would appear due to this feature the theme has created a bug where by limiting products shown on /shop, it also put the same limit on the search query.  We have decided to remove the category limit from /shop from this theme and it fixed the problem.  However, we have another option to create a custom function to remove the filter on the search page.
    To do this, change the code from 5) to the following://WooCommerce Search Query
    function woo_search_query( $query ) {
    if ( $query->is_search && $query->is_main_query() ) {
    // Hook the above function to the pre_get_posts action
    add_action( ‘pre_get_posts’, ‘woo_search_query’ );

Our Thoughts.

As you can see, WordPress is powerful and has lots of themes and plugins.  However, when something doesn’t work as expected, this can actually make it harder to trace the problem to the codes which caused the problem.  In this example the problem was a but in the theme which only happens when a certain theme option has been used causing unexpected search results.  Hopefully this post will help someone who needs to find a bug on their website.