WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use code to show custom field outside the loop in wordpress attachment page (image.php) it's returns nothing.

this is the code i'm used to show the custom field:

global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'my-custom-field', true);

and i'm trying to show parent post id in attachment page too with get_the_id and use this code

<?php global $wp_query;
$this_page_id = $wp_query->post_parent;
$post_id = get_the_id($post->ID);
echo $post_id ;?>

but the code above just shown the attachment ID not the parent post ID. anyone can help, please ?

share|improve this question
up vote 0 down vote accepted

You already have $post object on image.php. Don't complicate and use following:

echo get_post_meta($post->ID, 'my-custom-field', true);
echo $post->post_parent;
share|improve this answer
work well like i wanted, thnks Max – Advenda Feb 14 '13 at 9:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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