davidasor
davidasor

Reputation: 329

How to get all products that doesn't have feature image in woocommerce?

some of my products missing their images, what I'm trying to do is to make a query that will give me only products with the default image (woocommerce place holder image).

this is what i have tried :

$args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'meta_query', array( 
            array(
               'key' => '_thumbnail_id',
               'value' => '5',
               'compare' => '=='
            )
        )
    );

i found place holder image id using this function :

attachment_url_to_postid("/wp-content/uploads/woocommerce-placeholder.png");

the query returns every single product I have, and not only those with the placeholder image, what causes it, and is there a better way?

Upvotes: 1

Views: 1099

Answers (1)

Bhautik
Bhautik

Reputation: 11282

You can check using meta_key _thumbnail_id. Try the below query.

global $wpdb;

$post_ids = $wpdb->get_results( "
    SELECT ID FROM $wpdb->posts 
    WHERE ID NOT IN (
        SELECT post_id from $wpdb->postmeta 
        WHERE meta_key = '_thumbnail_id' 
    ) 
    AND post_type = 'product' 
    AND post_status = 'publish'
" );

echo "<pre>"; print_r( $post_ids ); echo "</pre>";

The _thumbnail_id is not NULL for these products, as you said - but a record with the meta_key _thumbnail_id does not even exist in the meta data table. This query filters out all products that have such a record.

Upvotes: 4

Related Questions