Join

Set a Default Featured Image for WordPress Posts

Don't want to mess with code snippets? This is a feature of MyListing Pro.

Instructions

  1. Create a new PHP code snippet.
  2. Copy the contents of code snippet below.
  3. Paste the contents into your code snippet.
  4. Review any notes that I’ve provided.
  5. Save and enable the code snippet.
  6. Test.

Snippet

Note: This solution applies to posts you create going forward. It does not retroactively assign the default featured image to posts you’ve already published.

Adjust Line 7, adding the full URL of the image you want to assign as the default featured image.

PHP
function mytheme_set_default_thumbnail( $post ) {
    if ( $post->post_type != 'post' ) {
        return;
    }

    if ( !has_post_thumbnail( $post->ID ) ) {
        $thumbnail_url = 'https://domain.com/wp-content/uploads/image-file-name';
        $thumbnail_id = attachment_url_to_postid( $thumbnail_url );

        set_post_thumbnail( $post->ID, $thumbnail_id );
    }
}
add_action( 'rest_after_insert_post', 'mytheme_set_default_thumbnail', 10, 3 );