0

I'm using the woocommerce-auto-category-thumbnails plugin to replace a blank category thumbnail with a random thumbnail from one of the products in the category (or child categories).

It is supposed not to use the thumbnail if the products have no thumbnails, using this query

   $query_args = array(
        'post_status' => 'publish',
        'post_type' => 'product',
        'posts_per_page' => 1,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'field' => 'id',
                'taxonomy' => 'product_cat',
                'terms' => $cat->term_id
            ),
            array(
                'field' => 'slug',
                'taxonomy' => 'product_visibility',
                'terms' => 'exclude-from-catalog',
                'operator' => 'NOT IN',
            ),
            array(
                'key' => '_thumbnail_id',
                'value' => '',
                'compare' => '!='
            )
        )
    );

to select products in the category.

However, the last term,

            array(
                'key' => '_thumbnail_id',
                'value' => '',
                'compare' => '!='
            )

have no effect, I've even tried

            array(
                'key' => '_thumbnail_id',
                'value' => '234',
                'compare' => '=='
            ) 

and I still get random images, sometimes the placeholder.

The query is being run, because if I change the $cat->term_id to a constant, I get images from the same category all the time.

I guess I could fetch more products and loop until I found one with a real image, but I find that rather wasteful.

Is it possible to fix the query to do what's wanted?

  • And how is the placeholder image stored? Is it just a default value for _thumbnail_id or does such post has no thumbnail set at all? – Krzysiek Dróżdż Dec 2 '18 at 9:02
  • No _thumbnail_id at all – Lenne Dec 2 '18 at 9:21
  • Try 'key' => '_thumbnail_id', 'compare' => 'EXISTS' – Max Yudin Dec 16 '18 at 13:20
  • Thanks @MaxYudin, but no difference :-( – Lenne Dec 16 '18 at 14:02
  • The main point is that the last filter term IS NOT BEING PROCESSED AT ALL! Is there a way to debug the query? – Lenne Dec 16 '18 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.