Opened 9 years ago
Closed 9 years ago
#36980 closed defect (bug) (wontfix)
get_the_ID () if used outside of a loop returns first post ID
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.5.2 |
Component: | Query | Keywords: | close |
Focuses: | Cc: |
Description
Test,
add var_dump(get_the_ID () );
in archve.php template outside of the loop and it will return first post from loop ID.
We might need
if (in_the_loop()) {.....
check within the get_the_ID () function https://developer.wordpress.org/reference/functions/get_the_id/
Change History (6)
#2
follow-up:
↓ 5
@
9 years ago
OK understandable but should't static page id be targeted with get_queried_object_id() than?
I am doing this hack becuase of this "bug/feature"
function _thz_post_id() { if (in_the_loop()) { $post_id = get_the_ID(); } else { $post_id =get_queried_object_id() == 0 ? 999999999999 :get_queried_object_id() ; } return $post_id; }
but in my opinion if get_the_ID() is caught outside of the loop it should not return anything or it should return the get_queried_object_id() instead of picking any items from the loop.
#3
@
9 years ago
- Component changed from Posts, Post Types to Query
but in my opinion if get_the_ID() is caught outside of the loop it should not return anything or it should return the get_queried_object_id() instead of picking any items from the loop.
Can you be more specific about what it means to be "outside" of the loop? Does the bug happen both before and after the loop?
#4
@
9 years ago
Yes , it is both before and after the loop we get first or last item from the loop ,
if you just place print_r(get_the_ID()); on top of the archive.php you get first item from loop ID,
thus it made no sense since if out of the loop than the archive is main not any of the items from the loop.
Here is the reference https://github.com/ThemeFuse/Unyson/issues/1582#issuecomment-222519762
#5
in reply to:
↑ 2
@
9 years ago
- Keywords close added
Replying to Themezly:
OK understandable but should't static page id be targeted with get_queried_object_id() than?
That would be the more correct method, yes.
Unfortunately there's years of code that has been written before those functions were available which relies upon the current behaviour, changing it isn't viable for backwards-compatible reasons.
#6
@
9 years ago
- Milestone Awaiting Review deleted
- Resolution set to wontfix
- Status changed from new to closed
I agree with DD32 here, I think fixing this would break is_singular
pages too many sites.
Thanks for opening the ticket @Themezly, feel free to open tickets for any further oddities or bugs you find.
This is unfortunately something that I'm fairly certain a lot of themes, widgets, and plugins rely upon.
For example, for a clearer version, on a static page - what would you expect
get_the_ID()
orget_post()
to return in the sidebar? I'm fairly certain a lot of items are expecting that to be the single page being viewed.