Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make an array of all products that has only the place holder 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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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>";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading