最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Can't check if a post has thumbnail adding filter to get_post_metadata()

programmeradmin3浏览0评论

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/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?

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/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?

Share Improve this question asked Jan 17, 2020 at 16:53 stckvrwstckvrw 2852 gold badges3 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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()) {
    the_post_thumbnail();
}

you can change this to

if(has_post_thumbnail()) {
    the_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.)

发布评论

评论列表(0)

  1. 暂无评论