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

Time:02-16

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?

CodePudding user response:

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>";
  • Related