wp query - How to filter on placeholder image

admin2025-06-05  1

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?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749135157a316657.html

最新回复(0)