I need to set fake thumbnails to all posts which have yet no a thumbnail. I use the code from the link core.trac.wordpress.org/ticket/40547#comment:1

But I can't check if a post already has a thumbnail inside of the first function mentioned.

If I try the has_post_thumbnail() that is

function wp40547_filter_post_thumbnail_id( $null, $post_id, $meta_key, $single ) {
    if ( '_thumbnail_id' !== $meta_key ) {
        return $null;
    if( has_post_thumbnail( $post_id ) == false) {
        $attachment_id = 'my_fake_id';
    return $attachment_id;
add_filter( 'get_post_metadata', 'wp40547_filter_post_thumbnail_id', 10, 4 );

I receive the 502 error, as I can understand because the has_post_thumbnail() calls the get_post_thumbnail_id() which in turns calls the get_post_meta() and the get_post_metadata() so we call ourselves

How to check in the case?

1 Answer 1


Option 1: force featured image on all posts

Use the save_post hook, which runs twice per "save" action if you have revisions enabled. You can verify it's the final run by checking that there is $_POST data (it's empty the first time around).

add_action('save_post', 'wpse_force_featured_image', 20, 2);
function wpse_force_featured_image($post_id, $post) {
    // If this is a "Post" and $_POST data is present
    if(count($_POST) > 0 && $post->post_type == 'post') {
        // Check if featured image is missing
        if(!isset($_POST['_thumbnail_id'])) {
            // Add your desired fallback featured image
            add_post_meta($post_id, '_thumbnail_id', 'my_fake_id');

You'll have to make sure that the ID is a real image in the Media Library. Also, this will only work for new Posts, and Posts you update after the code is added. Any old Posts will have to be updated one by one.

Option 2: update theme to include fallback

Wherever you're using the featured image in the theme, you could just add a fallback image. So for example, if in single-post.php you have something like

if(has_post_thumbnail()) {

you can change this to

if(has_post_thumbnail()) {
} else {
    echo '<img src="image_url.jpg" alt="alt text" />';

The benefit is, this will add a fallback immediately for all Posts - you won't have to go back and update them or add new ones to see this in action. (If you don't already have a child theme or a custom theme, you'll want to make one so you're not editing parent theme files.)

  • Thanks for the efforts! Sorry, I didn't mention about this in the question, but I'm looking for a solution without touching the theme or saving a data in the DB. Now I'm trying if(count(get_metadata( 'post', $post_id, '_thumbnail_id', $single = false )) == 0) { inside of the wp40547_filter_post_thumbnail_id() function, but I receive the Fatal error: Allowed memory size of XXXXXXXXX bytes exhausted .... Why and how to resolve?
    – stckvrw
    Commented Jan 20, 2020 at 8:28
  • You can't use that hook, because it doesn't get the meta value - it always shows null for the value. So, you'll need to search for a different hook.
    – WebElaine
    Commented Jan 21, 2020 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.