0

I have a post type that contains meta boxes. These metaboxes allow a user to select an item from a dropdown menu. All these items are IDs for other posts.

So in the theme, I am using get_the_post_thumbnail to retrieve the featured image from the post ID retrieved from these metaboxes. It works.

What doesn't work is that if a metabox is empty, get_the_post_thumbnail will automatically retrieve the thumbnail for the global post ID.

This is from the codex for its first parameter:

Post ID or WP_Post object. Default is global $post. Default value: null

How do I prevent this OR what is another way to solve this? I don't want to load the image from the default post. I want it to be empty if nothing specific is selected.

2
  • I don't understand why you don't just avoid get_the_post_thumbnail() if post ID from metaboxes is null, zero, empty, false or whatever other non-valid post ID your metabox may return. Can you post the you are using to get the data from metaboxes and display the thumbnails?
    – cybmeta
    Commented Jan 6, 2017 at 8:59
  • It may be complicated to share. I'm editing a previously developed theme. There is a class being used that retrieves the post ID from a metabox item that lists titles of specific post-type IDs. A user selects a title that that is from a specific post-type. That ID is passed to an object then used in the theme.
    – rpeg
    Commented Jan 7, 2017 at 21:22

1 Answer 1

1

As you are storing posts IDs in meta fields of another post (let's call this post "the main post"), you can get that meta field and if it is empty, just avoid the use of get_the_post_thumbnail().

(For the next examples, I suppose that you are stroing one post ID in one meta field of the main post; if this is not the case, just say it and explain what data format you are using).

For example, if you are in the Loop of the main post:

while( have_posts() ) {
    the_post();

    // used inside the loop, get_the_ID() returns current post ID in the loop
    // replace meta_field_that_stores_post_ID with the key of your meta field
    // last param is set to true, so get_post_meta() retuns empty string if the meta field doesn't exists
    $thumb_post_ID = get_post_meta( get_the_ID(), 'meta_field_that_stores_post_ID', true );
    if( ! empty( $thumb_post_ID ) ) {
        echo get_the_post_thumbnail( $thumb_post_ID );
    }

}

You could separated that logic from the template, for example using a template tag:

function cyb_get_the_post_thumbnail() {

    $thumb_post_ID = get_post_meta( get_the_ID(), 'meta_field_that_stores_post_ID', true );

    if( ! empty( $thumb_post_ID ) {
        echo get_the_post_thumbnail( $thumb_post_ID );
    }
}

And then use cyb_get_the_post_thumbnail() wherever you need it within the loop of the main post.

If you are not within the loop of the main post, you need to know, at least, the ID of the main post and do something like this:

$thumb_post_ID = get_post_meta( 254, 'meta_field_that_stores_post_ID', true );
if( ! empty( $thumb_post_ID ) {
    echo get_the_post_thumbnail( $thumb_post_ID );
}
1
  • I like your solution here. I found another solution but it is narrowly specific to the theme I am editing. However your solution seems broad and sensible enough.
    – rpeg
    Commented Jan 7, 2017 at 21:24

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.