WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 am trying to display first post of 3 categs on my index page. so for that i wrote a little function as follows:

function excateg($categ) {
$recent = new WP_Query("cat=$categ&showposts=1");
while($recent->have_posts()) : $recent->the_post();
$imageurl = get_post_meta($post->ID, 'post-img', true);

Afterwards the Html for showing the image and title and image.. but i am unable to get the custom field as above in $imageurl to get a value..

Help is appreciated!

share|improve this question
up vote 1 down vote accepted

You need to define the $post global variable if you want to access its ID property:

function excateg($categ) {
  global $post;
  $recent = new WP_Query("cat=$categ&showposts=1");
    $imageurl = get_post_meta($post->ID, 'post-img', true);

Use it after the_post() because that function will set up that variable.

Alternatively you can use the $recent->post property instead of $post->ID...

share|improve this answer

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.