0

I'm working on a CPT (custom post type) page template and would like to get the source of custom meta image.

For post_thumbnail's this is rather easy using wp_get_attachment_image_src(), but if I var_dump( $post->ID ) I don't see a value for my custom meta attachment image source.

So how can I get the ID of my custom-meta-image to get it's source? Thanks :)

UPDATE This is what I got going.

<?php $portfolio_image_one = get_post_meta( $post->ID, '_cmb_portfolio_image_one', true ); ?>
<?php $portfolio_image_two = get_post_meta( $post->ID, '_cmb_portfolio_image_two', true ); ?>
<?php $portfolio_image_three = get_post_meta( $post->ID, '_cmb_portfolio_image_three', true ); ?>

<?php if ( has_post_thumbnail() ) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large'); ?>
<a class="featured-image" href="<?php echo $large_image_url[0] ?>" title="' . <?php the_title_attribute('echo=0') ?> . '">
<?php the_post_thumbnail('medium', array('class' => 'img-polaroid')); ?>
</a>
<?php } ?>

Basically, I would like to get the ID for my post_meta images in a similar way to the thumbnail approach.

2
  • What matters is what you're storing in the custom field, not how you're getting it. It's common to store the ID for images, but I'm guess you aren't doing that. What are you storing in the meta fields?
    – mrwweb
    Commented Sep 28, 2012 at 2:48
  • Not the ID :), I was hoping not having to do that manually. I found a couple of ways by matching $wpdb fields. But I also found a better way not using the IDs at all. If I find some time I'll post what I found out.
    – Marc Wiest
    Commented Sep 29, 2012 at 17:45

1 Answer 1

3

Custom Meta Image? What's that?

Do you mean that,

  • you are inserting an image into a custom field (post meta field) within your meta box?

  • you are inserting an image as a post attachment or thumbnail?

These are two very different things one of which is dealing with,

add_post_meta

the other,

wp_insert_attachment

The latter of which inserts attachments into your media library which is associated with your wp_posts database table.

Post meta on the other hand (i.e. add_post_meta etc) is associated with your wp_postmeta' database table.

Attachments are actually classified as a post_type just line post or page or your custom_post_type for instance.

Attachments, as a post type, may or may not also then be associated with a Post's ID, stored as an integer in the post_parent column in your wp_posts database. If set to 0, this means that the attachment has no parent and is not associated with any particular post.

Theory aside...

You may very well be trying to access an attachment that simply doesn't exist in the first place.

$attachment_id = 20; //example ID of the attachment you want to retrieve
$attachment = wp_get_attachment_image_src( $attachment_id );
echo $attachment[0];

The particular code above is useful for retrieving an attachment which is not associated with any particular post.

$post_id = 100; //example post ID
$attachment = wp_get_attachment_thumb_url( $post_id );
echo $attachment; //returns the URL of the image attached to the post

Where as retrieving an value, regardless of what that value is, when dealing with post meta (as you referred to as custom meta image) requires the use of,

get_post_meta

In use,

$post_id = get_the_ID();
$meta = get_post_meta($post_id, 'your_meta_key');
echo $meta;

API References

UPDATE

Based on your code,

$portfolio_image_one = get_post_meta( $post->ID, '_cmb_portfolio_image_one', true );

The post_meta value is held within your variable,

$portfolio_image_one

What you need to do is,

echo $portfolio_image_one;

Otherwise nothing is going to happen.

If there is a value stored for the meta_key i.e. _cmb_portfolio_image_one then by echoing the result of the variable, you shall be presented with your meta_value whatever that value is, such as http://domain.com/my_image.jpg for example.

If $post->ID is being used outside of the loop you will run into trouble, hence refer to my previous example, but with your code,

$post_id = get_the_ID();
$portfolio_image_one = get_post_meta($post_id, '_cmb_portfolio_image_one', true);
echo $portfolio_image_one;
2
  • Hey thanks for the elaborate reply, unfortunately it doesn't answer my question. I'll update my post to show you what I'm working with.
    – Marc Wiest
    Commented Sep 27, 2012 at 18:28
  • 'get_post_meta($post_id, '_cmb_portfolio_image_one', true);' is returning the full image url. how will you get the 'medium' size url for example?
    – Ben
    Commented Nov 14, 2016 at 8:45

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.