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!
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
$idwhich is a global var in WordPress and typically available within The Loop.
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!
the_ID() does not return the value, it just echoes or prints it. With PHP there’s a difference between the two.
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?
- The topic ‘Weird the_ID error’ is closed to new replies.