Check out our popular industrial, construction and manufacturing WordPress theme, Production Pro

Summer sale!
$29
$59
for any single
WordPress theme
$49
$99
for every theme
in our club package
$149
$299
for every theme and
lifetime support


Get the latest Create and Code updates and offers every month
Home Blog How to fix broken featured products in WooCommerce 3.0

How to fix broken featured products in WooCommerce 3.0

11 Comments

How to fix broken featured products in WooCommerce 3.0 11 Comments

This post covers how to fix broken Featured Products in WooCommerce 3.0. If you’ve been displaying WooCommerce Featured Products for some time and they are suddenly no longer displaying at all or appear to be displaying the wrong products, chances are you’ve recently upgraded to WooCommerce 3.0 and your shop is running out of date methods for retrieving WooCommerce products. Well fear not, we’re here with a simple fix to get your featured products displaying correctly again 🙂

Important changes to how Featured Products are stored in WooCommerce 3.0 (and later 3.0.X versions)

WooCommerce 3.0 made some important changes to how Featured Products are stored in the WordPress database. In WooCommerce versions prior to 3.0 including all versions of WooCommerce 2.X, Featured Products were flagged in the database using metadata stored in the wp_postmeta table. In WooCommerce 3.0.0, a new product visibility taxonomy was introduced.  This product visibility taxonomy includes catalog, search, hidden, featured and outofstock terms. This means that any themes or plugins that make the assumption that featured products are flagged using the older metadata method will no longer work! I must admit I was caught out by this despite extensive testing of the WooCommerce 3.0 betas. The main reason being that in our case we we’re still seeing featured products being displayed despite the older WP queries no longer being valid – so the change wasn’t a complete breaking change in our case. It took a little bit of digging into the WooCommerce 3.0 internals to discover the data structure change 🙂

So why was the change introduced in WooCommerce 3.0?

A big focus for the WooCommerce dev team in 3.0 and especially in the upcoming 3.1 release is performance improvement. When you operate at WooCommerce at scale, there are clear, major pain points and bottlenecks related to how WooCommerce has traditionally stored data. Those pain points and bottlenecks are being actively addressed by the WooCommerce development team at present which is great news for all WooCommerce store owners and essential if WooCommerce is to continue to grow market share with larger online stores. Doing queries on meta data requires extra SQL joins and is quite inefficient at scale. Querying WP terms is much more efficient and as a result, much faster. And a faster loading WooCommerce store means happier customers and increased sales 🙂

How to fix the display of featured products in WooCommerce 3.0

So if you’ve landed on this post chances are you’re currently running a theme or plugin that uses the old postmeta based method of displaying your featured products. Your query arguments probably look something like this:

Note the query arguments meta_key and meta_value? They’re the cause of your problem. Those values are no longer valid.

The query arguments above should now be replaced with a new tax_query argument, which would look like the following:

If you’re using a commercial theme or plugin my advice is to contact the original theme/plugin author and let them know they need to update their code – most of the good developers will be keen to ensure they’re running the latest compatible code 🙂

 

Follow us on Twitter and Facebook!

Stay connected – be the first to get interesting updates and new releases from Create and Code.

Colm has been building web stuff since the web was a wee lad. Colm built his first blog with WordPress around 2007 and has been hooked ever since. When not knee deep in hooks and filters you'll probably find him running around a mountain or making pizza.

11 comments

  1. This is my exact problem at the moment. I may not be as technically adept as the others on this board, so could you tell me what file I’m modifying with the above?

  2. Thank you..

    It’s Working..
    My code :

    $query_args[‘tax_query’] = array();

    $query_args[‘tax_query’][] = array(
    ‘taxonomy’ => ‘product_visibility’,
    ‘field’ => ‘name’,
    ‘terms’ => ‘featured’,
    );

    1. Hi there,
      There’s no one single file that needs to be updated unfortunately. It could be in the theme or it could be in a plugin. The best advice is to search your codebase for an out of date line like 'meta_key' => '_featured'. If you find that then you’ll know what needs to be updated – but as per the post – I would recommend contacting the plugin/theme author and get them to fix it 🙂

  3. I have problem with old template :-(, Maybe someone will help me.

    if($type == ‘feautured’){
    $args = array( ‘post_type’ => ‘product’, ‘orderby’=>’rand’, ‘posts_per_page’ => $showpost ,’meta_query’ => array(
    array(
    ‘key’ => ‘_visibility’,
    ‘value’ => array(‘catalog’, ‘visible’),
    ‘compare’ => ‘IN’
    ),
    array(
    ‘key’ => ‘_featured’,
    ‘value’ => ‘yes’
    ) ), ‘tax_query’ => array(
    array (
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘id’,
    ‘terms’ => $categories
    ),
    ) );

    $pc = new WP_Query( $args );

    }

Leave a Reply

Your email address will not be published. Required fields are marked *