Get the ID of a Post Meta Image
I Google’d and checked the WordPress codex, but could not find a function that would give me the ID of an image I uploaded into a custom meta field.
I have uploaded an image, but that image is huge. I want to be able to display the image at my predefined size. There are functions to display the post thumbnail (featured image) in any which way you want, but this is different.
This is the cleanest method I’ve found to get the attachment’s ID. With the ID, you’ll be able to use a function to display that image.
global $wpdb; $image_url = get_post_meta(get_the_ID(), _custom_image, TRUE); $image_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM wp_posts WHERE guid='%s';", $image_url ) );
Once you have the image’s ID, you can use wp_get_attachment_image:
echo wp_get_attachment_image( $image_id, 'custom-image-size' );
Strange how WordPress doesn’t have a function for this… maybe I’m wrong — let me know!