Title: Cannot get Post ID
Last modified: August 31, 2016

---

# Cannot get Post ID

 *  Resolved [alexejp](https://wordpress.org/support/users/alexejp/)
 * (@alexejp)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/cannot-get-post-id/)
 * hey there!
 * i code a plugin.
    in this little piece of code i want to create an array which
   contains the title of each post as Key, this actually works great. but the post
   id is just not added as value to each key. Dont get the point in it, been looking
   for mistakes for ages, or at least felt like that.
 *     ```
       $heldengeschichtenDropdown = array( 'Platzhalter' => 0 );
       $args = array( 'category' => 5 );
       $heldengeschichten = get_posts( $args );
       foreach ( $heldengeschichten as $post ) : setup_postdata( $post );
       	$arraykey = get_the_title($post);
       	$value = $id;
       	$heldengeschichtenDropdown[$arraykey] = $value;
       endforeach;
       wp_reset_postdata();
       ```
   
 * [Moderator note: code fixed. Please wrap code in the backtick character or [use the code button](https://codex.wordpress.org/Forum_Welcome#Posting_Code).]

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

 *  [girlieworks](https://wordpress.org/support/users/girlieworks/)
 * (@girlieworks)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/cannot-get-post-id/#post-7163248)
 * [@alexejp](https://wordpress.org/support/users/alexejp/), try including `global
   $post;` in your code, per [the Codex article for setup_postdata](https://codex.wordpress.org/Function_Reference/setup_postdata)(
   there are examples in that article if you’re not sure where you should put it).
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/cannot-get-post-id/#post-7163254)
 *     ```
       $value = $post->ID;
       ```
   
 *  Thread Starter [alexejp](https://wordpress.org/support/users/alexejp/)
 * (@alexejp)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/cannot-get-post-id/#post-7163266)
 * Thanks very much for your help!
 * including
    `global $post;` didn’t help. But `$post->ID;` makes the magic happen!
 * that `global $post;` didn’t work quite surprised me pretty much.
 * whats about this arrow `->` notation? is it like $post is an object with attributes
   and one of these atts is ID?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/cannot-get-post-id/#post-7163276)
 * > is it like $post is an object with attributes and one of these atts is ID?
 * Exactly! Though with objects, the values are usually referred to as ‘properties’
   instead of ‘attributes’, but it’s just semantics.

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

The topic ‘Cannot get Post ID’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 4 replies
 * 4 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/cannot-get-post-id/#post-7163276)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
