If you’re on a singular page, sometime after init
and all the query variables have all ben set up you can use get_queried_object_id
or get_queried_object
.
<?php
if (is_singular()) {
$post_id = get_queried_object_id();
// or get the whole object
$post = get_queried_object();
// or do the first one differently
$post_id = get_queried_object()->ID;
}
You can also just “false start” the loop and get what you need. It probably won’t slow down your script: WordPress fetchs all the queried posts at once, so the database hit has already happened. You might use this if you’re not a singular page and need to get the first post’s ID.
<?php
// start the loop
the_post();
// get the ID
$post_id = get_the_ID();
// back to normal
rewind_posts();
Related Posts:
- Get page ID of page that is set as the posts page
- Are post ID’s reliable?
- Get current post id in functions.php
- get all posts ID from a category
- Get the current post ID as a variable in Javascript
- Is post ID number always incremental n+
- Are all ID’s used unique?
- $post->ID displays wrong ID
- At my posts archive page, outside the loop, get_the_id() returns the top most post’s ID
- Showing random content / pictures from earlier posts in a sticky post?
- 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?
- Undefined variable post_id in custom quick edit coloumn
- Getting post id from wp_insert_post_data function?
- Does an article (post) id ever change?
- How to get the post’s parent ID?
- Can a page_id and a post_id be same?
- Is there a better, more efficient way to get the post id outside the loop?
- Getting current post ID in functions.php
- Buddypress activity id
- How blog page in WordPress works : blog page retrieve first post ID
- Post-ID in url differs from $post->ID
- Why is my Blog Page ID == First Post ID?
- current post with current author
- Is it possible to change post id for an already added menu item in WordPress?
- Get ALL post ID’s export list (Only id’s. Php or sql not important )
- How to get next post ID?
- Search results posts_orderby and ID
- Retrieve post ID from “querying” URL
- How to get a post’s content? [closed]
- obtain the author id given the post id
- How to get ID of the page included with get_page()?
- How to I retrieve the ID from the Posts page?
- Crazy Question – Updating Post ID
- Why I can not I use the variable outside my function?
- retrieve the oldest post id
- WP_Query custom field pass the post id
- Accessing Post ID Within Loop
- How do post IDs work in WordPress?
- Are post, page and category IDs unique to each other?
- The loop starting at a certain ID
- linking to post outside the loop
- Widget to show posts in the sidebar basing on its IDs
- Making posts permalinks consistent numbers
- How to get post id of last approved comment?
- How to get the post id (of the parent custom custom type) in a loop inside a widget?
- Get post slug and match with menu item slug to change css
- WordPress Post ids big increment
- Reuse old post ID for new post after deleting post
- Get post attachment with post id
- How can i take all ids from untrash_post action?
- How do I apply a wordpress function to something with no ID inside an archive page?
- Get from the dashboard the ID of the current post being edited
- Only get post_id [duplicate]
- Surrogate ID for posts, is there an alternative field in the posts table?
- Is it possible to have dynamic post id # in add_menu_page()?
- Using Post ID and Page ID in same function
- unused post IDs
- Custom post page has attributes of latest post [closed]
- Can I get custom post items in select box (dropdown)
- What is $post->ID
- View post with specific category id and name which I selected in the backend (drop-down option)
- How to get post ID after removing that post?
- Accessing the post content with WP_Query
- How do I use inline SVG in WordPress
- Add a special filter link to All Posts in admin
- Use Cron to modify posts via sql
- Pull posts from another wordpress install on same server
- Create pretty permalink for a post knowing the permalink structure
- jQuery inluclude still seems ncessary for script to work within post
- White Blank Page when Updating and Publishing Page/Post
- Why is querying posts messing up my pages?
- How can I find out what items a user has purchased? [closed]
- WordPress Loop – Next 3 Posts
- How to do set post permalinks using 6 digit random unique function?
- Encountering “Wrong nonce. Action prohibitied.” when trying to alter User Role and unable to Post via WP Admin
- Issue where WP Featured Image will not display
- Display post category in foreach loop
- Single.php – Get Current Parent Category
- insert a warning message into post-new.php
- WP text editor change html markup
- Is it possible to give the user the chance to select between two single template files to use in a post?
- Modifying the following code to displaying the title and the content of static page Posts page?
- Date is wrong on ‘all posts’ page
- How do I disable the “by author” hyperlink on posts?
- Jumbled writing under a post
- Featured Images most often doesnt appear
- Why is WordPress showing a blank excerpt for just one of my posts?
- Is there a way to know when a page has been updated and do some action only once?
- Contact Form 7 Shortcode not recognized inside another shortcode
- WordPress Pods Custom Post Type – separate Media Upload folder for each custom Post Types
- Display new posts categories in separated divs
- update post category in a new table on frequent change of category
- Edit Posts Page but not category specific pages?
- How can add metabox for post of specific category before save post and after save post [duplicate]
- Posts page template: How can I edit the markup for this?
- Pagination working locally but not on live site
- Should new posts already have tags?
- Is there any point to using wp_unique_post_slug?