Have you been trying to coax a plugin to sort or filter your website’s WooCommerce products and been mystified when products seem to be missing? We have too, and although there could be many reasons your products are not found we thought we would share our solution. Our particular scenario involved trying to use the Pagelines PostGrid plugin, but many situations will be similar.
The Problem: Not Finding Products
We discovered some of our products were not being found when we wanted to only show published or visible products. In your situation you might not have any products appear at all. Confusingly, the filter was only finding products published BEFORE or during October 2017. (This would be important later.) We dug into the code and found the database query that was failing us. However, the WordPress query appeared to be in order. As you can see from the example below the query checks the meta key “_visibility” and attempts to verify that the the value is either “catalog” or “visible”.
// ... $args['meta_query'] = array( array( 'key' => '_visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN' ) ); // ... $query = new WP_Query($args);
The Cause of the Problem
The reason our filter wasn’t returning all the expected products was because of a WooCommerce update in WooCommerce version 3.0. WooCommerce decided that it would be better for performance to stop storing the public/hidden flag in post meta and instead use taxonomies. The code example above uses the old post meta query instead of the new taxonomy query. If you have only started using WooCommerce since version 3.0 the you might not have any products show up at all. If you were using it before version 3.0 like we were then only those old products will show up.
The Solution to Finding All Products
The answer is simply to start using the new taxonomy query to filter or sort your products. The main difference is using $args[‘tax_query’] instead of $args[‘meta_query’] and the appropriate fields. This should be a simple drop-in replacement for the code snippet above. But remember that the PHP code you are dealing with may have its own idiosyncrasies!
// ... // Define the taxonomy query condition that checks product visibility $args['tax_query'] = array( array( 'taxonomy' => 'product_visibility', 'terms' => array('exclude-from-catalog'), 'field' => 'name', 'operator' => 'NOT IN', ) ); // ... $query = new WP_Query($args);
Finding All Products!
In summary, your WordPress filtering/sorting might not find your WooCommerce products if your query uses the deprecated “_visibility” post meta. Instead, you should update your queries to use WooCommerce taxonomies.