The get_the_ID()
function uses the global $post
variable, which outside of the loop is sometimes equal to get_queried_object_id()
. Within the loop, the $post
variable is set to the current post within the loop.
As to why get_queried_object_id()
is recommended outside of the loop, is because it does not use the global $post
variable, so is less likely to be corrupted by loops.
Related Posts:
- How to get posts published between a date and today?
- Display all posts from specific categories on a page
- How to exclude latest x posts from a paginated query?
- Display content from a specific category
- Can’t show comments count per post outside loop
- Showing random content / pictures from earlier posts in a sticky post?
- How to Display Post View Count
- How would I get 1 latest post from a query for 5 posts?
- Inserting custom data to the_post() during loop
- How To Get Posts With A Specific Word
- Why WP_Query(‘showposts=5’) shows only 1 post?
- Display all posts that were published before full post on single.php
- Blog post per page setting conflicting with custom WP_Query?
- How to exclude latest x posts from a paginated query?
- Two loops, one AJAX loop, exclude posts in first loop from second loop, loops are in different files
- How to order posts in an arbitrary order?
- Display Posts on Custom Page
- Wrong post title displayed from loop
- Get the amount of posts on a current page
- How to get posts published on the latest date?
- How to only publish posts with image in it
- WP_Query: Show 10 posts in date order, first three random
- Accessing Post ID Within Loop
- Display specific posts on home page
- The loop starting at a certain ID
- linking to post outside the loop
- Display 3 posts with different HTML markup using a loop
- How to show post views shortcode data for each post on Posts List Page?
- Use special template for the first post in the loop
- Modify WordPress loop after it has been run
- show latest authors blog post
- Why I obtain this WP_Query strange behavior trying to select posts having a specific tag?
- Adding a category at even positions on main loop with modified pagination
- How to inject a post within a loop
- View post with specific category id and name which I selected in the backend (drop-down option)
- WordPress Post Query Using Custom native MySQL like St_Distance_Sphere
- Get page ID of page that is set as the posts page
- Print Current Post Index number within Loop
- Should we trust the post globals?
- Keep featured content post in homepage with original order
- How to show related posts by category
- Are post ID’s reliable?
- Get current post id in functions.php
- How set featured posts using checkbox in post edit screen?
- the_author() returns empty string
- Get first post from wp_query
- get all posts ID from a category
- Do we still need to include a “if (have_posts())” in templates?
- WP_Query: Why is sticky post not first item in loop?
- Query *only* sticky posts
- rewind_posts() – what actually the use of it, and where using is required or preferred?
- Skip post in loop and mixin later
- How to show list of posts by author and category?
- Get the current post ID as a variable in Javascript
- Is post ID number always incremental n+
- Override the default number of posts to show for a single loop?
- get comments and get posts in loop
- How do I reorder (pop and push) items from WP_Query?
- How to display posts by current user/author in a custom page template?
- post_name empty after wp_insert_post
- Reverse chronology of post listing
- Query posts distinct authors
- Why do I need to use The Loop on pages (inside page.php etc)?
- How to get previous 10 days post from a specific date – WP Query
- posts page – different lengths of excerpt
- Number of posts per page setting is not working?
- WordPress Number of Posts Not Changing With posts_per_page
- how to display full post with pagination on home page
- Are all ID’s used unique?
- Count singular post views automatically
- Looping through posts per category gives same posts for each category
- WordPress Posts Out Of Order after 3.1 Update
- $post->ID displays wrong ID
- Find most recent authors
- Query only Posts from Both of Two Category?
- This AJAX Code Doesn’t Work – Looking for elegant solution
- Insert custom div between posts
- Display All Sticky Post Before Regular Post
- Cannot access current post’s ID in custom plugin
- At my posts archive page, outside the loop, get_the_id() returns the top most post’s ID
- Get the author of the latest revision
- How to have different content in the loop and single
- Only display posts with comments
- Specific loop in Shortcode
- Strange switch_to_blog() issue
- Show a list of recently viewed posts to a user
- Multiple loops in same page, without duplicate content
- loop not displaying posts on custom template page
- Repost post on specific date every year
- How to use alternate post layout at key points in post loop?
- Alternate custom content in the loop
- Order post by year DESC and month ASC
- Search widget breaks when using multiple loops?
- WordPress get posts by date without query_posts
- Get the post_id of a new post
- get post id in while loops outputting page id
- How can I display a specific user’s first published post?
- Different amount of posts on homepage than paged pages
- Undefined variable post_id in custom quick edit coloumn
- Displaying the number of posts in a custom loop (without including the number of posts of a loop below)?