PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

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

 March 03, 2022     php, product, woocommerce, wordpress     No comments   

Issue

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?


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


Answered By - Bhautik
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing