Title: Retrieving ID from query_posts()
Last modified: August 19, 2016

---

# Retrieving ID from query_posts()

 *  Resolved [davejay42](https://wordpress.org/support/users/davejay42/)
 * (@davejay42)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/retrieving-id-from-query_posts/)
 * I’m running query posts to get the posts for a specific category, it’s working
   fine, but I also need to know the ID of the current post iteration so that I 
   can pull a custom value for the particular post that I’m on.
 * When I try using $post->ID it returns the post id for the page that I’m on, not
   the one from my query_posts() loop. I’ve also tried using get_queried_object_id(),
   but that just throws up an error saying it’s an unrecognized function.
 * Here’s my code:
 *     ```
       <?php query_posts('category_name=upcoming-events&showposts=10'); ?>
   
          <?php while (have_posts()) : the_post(); ?>
       	<div class="module_row">
       	   <h4><?php the_title(); ?></h4>
       	   <h5><?php get_post_meta(30, 'upcoming_event_date', true); ?> test</h5>
       	   <div class="hr"><hr /></div>
       	</div>
   
       	<?php the_content(); ?>
   
       	<?php edit_post_link('Edit This Post', '<p class="right">', '</p>'); ?>
   
          <?php endwhile; ?>
       ```
   
 * In the get_post_meta() function, you’ll see, I need the ID field to be dynamic,
   not hard coded in.
 * Actually, this problem may extend beyond being unable to retrieve the ID. Even
   with it hard coded in as you see above `get_post_meta(30, 'upcoming_event_date',
   true);` It still doesn’t display the custom field I’m trying to pull out “upcoming_event_date”,
   and I’m positive that’s the correct ID too.
 * I’m at a loss, any help would be greatly appreciated. Thanks!

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

 *  [soundandshape](https://wordpress.org/support/users/soundandshape/)
 * (@soundandshape)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/retrieving-id-from-query_posts/#post-839592)
 * I am also trying to figure out how to generate a list of IDs dynamically.
 * Instead of hardcoding the IDs into the page template, I need code that will dynamically
   return an array of the IDs of categories that are currently children of a certain
   parent category.
 * something like get_category_IDs(child_of=’ParentID’)?
 * If you figure this one out, please let me know!
 * c.
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 9 months ago](https://wordpress.org/support/topic/retrieving-id-from-query_posts/#post-839597)
 * [@davejay42](https://wordpress.org/support/users/davejay42/): `$current_id = 
   get_the_ID();`
 *  Thread Starter [davejay42](https://wordpress.org/support/users/davejay42/)
 * (@davejay42)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/retrieving-id-from-query_posts/#post-839605)
 * [@otto42](https://wordpress.org/support/users/otto42/): Thanks a ton! That worked.
 * For the record, here’s what I’m using now that works.
    `echo get_post_meta(get_the_ID(),'
   upcoming_event_date', true);`

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

The topic ‘Retrieving ID from query_posts()’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [davejay42](https://wordpress.org/support/users/davejay42/)
 * Last activity: [17 years, 9 months ago](https://wordpress.org/support/topic/retrieving-id-from-query_posts/#post-839605)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
