Title: Custom Fields Problem
Last modified: August 19, 2016

---

# Custom Fields Problem

 *  Resolved [doppyo](https://wordpress.org/support/users/doppyo/)
 * (@doppyo)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-problem-2/)
 * Hey WP gurus, here’s the problem that might get you a beer from me if you get
   to resolve it :
 * My homepage has multiple loops. Within one of them, I’m sorting out five post
   from one category in a table. Next to the post’s title I want to get the post’s
   custom fields. Thing is, it seems impossible to get the current post’s id through
   $post->ID, in fact, the $post variable is empty so there’s no way I can get my
   custom field displayed. Here’s the code :
 *     ```
       <?php $my_query = new WP_Query('category_name=catname&showposts=5');?>
       ...some html...
       <?php while ($my_query->have_posts()) : $my_query->the_post();?>
       <tr>
       <td><?php the_title(); ?></td>
       <td><?php $key = "key1"; echo get_post_meta($post->ID,$key, true); ?></td>
       </tr>
       <?php endwhile; ?>
       ```
   
 * I tried to print out the $post->ID and it is empty. I also tried to replace $
   post->ID in the code above with a random post’s id like get_post_meta(_13_,$key,
   true); and it works perfect so the problem here is getting the current post id
   properly. the_ID() works just fine but I can’t put its return value inside a 
   $variable for some reason. Any help would be greatly appreciated.
 * Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-problem-2/#post-1216147)
 * Try `$my_query->post->ID`
 *  Thread Starter [doppyo](https://wordpress.org/support/users/doppyo/)
 * (@doppyo)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-problem-2/#post-1216156)
 * Works flawlessly, thanks a lot, Michael!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom Fields Problem’ is closed to new replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [doppyo](https://wordpress.org/support/users/doppyo/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-problem-2/#post-1216156)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
