WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] post id as php variable? (6 posts)

  1. hofo
    Member
    Posted 8 years ago #

    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

  2. ringmaster
    Member
    Posted 8 years ago #

    $wp_query->post->ID

  3. davidhouse
    Member
    Posted 8 years ago #

    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.

  4. ringmaster
    Member
    Posted 8 years ago #

    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;
    }

  5. hofo
    Member
    Posted 8 years ago #

    Thanks!

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

  6. PJ Brunet
    Member
    Posted 7 years ago #

    Thanks ringmaster.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags