Support » Fixing WordPress » post id as php variable?

  • Resolved hofo



    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





    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.



    Yeah, like:

    function some_func() {
    // Works inside of the loop
    global $post;
    $thePostID = $post->ID;


    function some_func() {
    // Works in single posts outside of the loop
    global $wp_query;
    $thePostID = $wp_query->post->ID;




    (!#@#!@#@$%! case sensitive languages, grumble, grumble…)

    Thanks ringmaster.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘post id as php variable?’ is closed to new replies.