Join

Set a Default Featured Image for WordPress Posts

Don't want to mess with code snippets? Request for this to be 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 will not retroactively assign the default featured image to posts. This applies to posts you create going forward.

Adjust Line 9, 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 );