Title: post id as php variable?
Last modified: August 18, 2016

---

# post id as php variable?

 *  Resolved [hofo](https://wordpress.org/support/users/hofo/)
 * (@hofo)
 * [20 years, 10 months ago](https://wordpress.org/support/topic/post-id-as-php-variable/)
 * Hey,
 * How do I get to the post ID as a PHP variable? I’ve tried in and outside the 
   loop, $thePostID = $post->id; but this doesn’t seem to be working. I’m trying
   to get the post id so I can then call get_post_meta to retrieve the value of 
   a custom field called “filename”.
 * Thanks for any help…
 * — hofo

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

 *  [ringmaster](https://wordpress.org/support/users/ringmaster/)
 * (@ringmaster)
 * [20 years, 10 months ago](https://wordpress.org/support/topic/post-id-as-php-variable/#post-237104)
 * $wp_query->post->ID
 *  [davidhouse](https://wordpress.org/support/users/davidhouse/)
 * (@davidhouse)
 * [20 years, 10 months ago](https://wordpress.org/support/topic/post-id-as-php-variable/#post-237105)
 * It’s $post->ID, not $post->id. Yes, that does matter. Also make sure that if 
   you’re doing it from inside a function, that $post is declared as a global variable.
 *  [ringmaster](https://wordpress.org/support/users/ringmaster/)
 * (@ringmaster)
 * [20 years, 10 months ago](https://wordpress.org/support/topic/post-id-as-php-variable/#post-237106)
 * Yeah, like:
 * `function some_func() {
    // Works inside of the loop global $post; $thePostID
   = $post->ID; }
 * or:
 * `function some_func() {
    // Works in single posts outside of the loop global 
   $wp_query; $thePostID = $wp_query->post->ID; }
 *  Thread Starter [hofo](https://wordpress.org/support/users/hofo/)
 * (@hofo)
 * [20 years, 10 months ago](https://wordpress.org/support/topic/post-id-as-php-variable/#post-237107)
 * Thanks!
 * (!#@#!@#@$%! case sensitive languages, grumble, grumble…)
 *  [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [19 years, 8 months ago](https://wordpress.org/support/topic/post-id-as-php-variable/#post-237735)
 * Thanks ringmaster.

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

The topic ‘post id as php variable?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 4 participants
 * Last reply from: [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * Last activity: [19 years, 8 months ago](https://wordpress.org/support/topic/post-id-as-php-variable/#post-237735)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
