Weird the_ID error (5 posts)

  1. Stephen Rider
    Posted 9 years ago #

    For this example: let's say I'm on Post with ID 4. In the loop, I have the following line:

    echo get_post_meta(the_ID(), 'thisPageIndex', true);

    This should echo the content of tag 'thisPageIndex' for the post with ID 4. Instead it echoes:


    If I hard-code the post number in, like this:

    echo get_post_meta('4', 'thisPageIndex', true);

    It properly echoes the value of the 'thisPageIndex' tag.

    I can hard code it as number or string and it works fine. I tried wrapping the_ID in an strval() -- no dice. Any time I try to call the post's ID with the_ID, the whole line is overwritten with just the ID number. I can't figure out what could possible be causing this! HELP!

  2. Kafkaesqui

    Posted 9 years ago #

    This is actually expected behavior for the_ID(). It's goal is to echo the ID of the post, and it appears to be doing that quite well. So to return or pass the post ID to the get_post_meta() function, use this instead:


    You can also try $id which is a global var in WordPress and typically available within The Loop.

  3. Stephen Rider
    Posted 9 years ago #

    Thanks for the assist. Either of those seems to work.

    The behavior of the_ID() does seem to fly in the face of logic though -- if the function returns the post ID, then why would it overwrite an entire other function containing it?

    Whatever -- It's working now. Thanks a bunch!

  4. Kafkaesqui

    Posted 9 years ago #

    the_ID() does not return the value, it just echoes or prints it. With PHP there's a difference between the two.

  5. mahalie
    Posted 8 years ago #

    I was trying to assign the_ID() to a var to use in a custom query and it was driving me nuts since echoing was working fine. I don't really understand it but $id worked.

    However, if someone wants to explain that, more specifically, how could/would you store the results from the_ID() in a variable so you could use it as an int?

Topic Closed

This topic has been closed to new replies.

About this Topic