How to add WordPress featured image via SQL

I can tell how to do it with WordPress functions, which is quite easy and you won’t lose any important stuff:

  1. Upload the default.jpg image using WordPress media manager. Note the ID of the image.
  2. Get all posts from your custom post type and run set_post_thumbnail() for each one.

Example: add this code to functions.php of your theme or in a plugin. Visit the admin side of you site to make it run. Delete it after the job has been done (otherwise it will be executed every time the admin area is visited).

add_action('admin_init', function () {
    //Replace with the correct image ID
    $image_id = 45;
    $args = array(
        'nopaging'   => true,
        'post_type'  => 'listings'
    );
    $listings = get_posts( $args );
    foreach( $listings as $listing) {
        //if has not featured image assigned, set default
        if( ! has_post_thumbnail($listing->ID) ) {
            set_post_thumbnail( $listing->ID, $image_id );
        }
    }
} );

Alternative: don’t set default image for every post. Instead check if post has thumbnail (featured image), if not display default.jpg:

//Assuming we are inside the loop
if( has_post_thumbnail() ) {
    the_post_thumbnail();
} else {
    echo '<img src="http://url.com/to/default.jpg">';
}