Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I would use WP_Query:

    $args = array(
    	'post_type' => 'post',
    	'post_status' => 'publish',
    	'meta_query' => array(
    		array(
    			'key' => '_thumbnail_id',
    			'value' => '?',
    			'compare' => 'NOT EXISTS'
    		)
    	),
    );
    $the_query = new WP_Query( $args );

    If you really need the SQL query string, the above query more or less generates this one:

    SELECT wp_posts.* FROM wp_posts
       LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id
          AND wp_postmeta.meta_key = '_thumbnail_id')
       WHERE wp_posts.post_type = 'post'
          AND (wp_posts.post_status = 'publish')
          AND ( wp_postmeta.post_id IS NULL )
       ORDER BY wp_posts.post_date DESC

    Thread Starter mindfl0w

    (@mindfl0w)

    Worked flawlessly, thanks so much!

    Kingsley Felix

    (@iamkingsleyf)

    How do we run the code?

    Moderator bcworkz

    (@bcworkz)

    To use this code, refer to Class Reference/WP Query for use of the WP_Query class. The first “standard loop” example would take the above $args array verbatim to run the query as discussed here. This code typically would be on a custom template of some sort.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘SQL qiery to get all posts without featured image’ is closed to new replies.