You can make use of get_queried_object() here, which is a wrapper for $wp_query and returns the whole post metadata.
Here’s a sample code:
$queried_object = get_queried_object();
if ( $queried_object ) {
$post_id = $queried_object->ID;
echo $post_id;
}
Related Posts:
- widget should display post archive by year and on click also by month
- Which file displays the date archive?
- Get all posts by post_author
- Displaying a widget only on long posts (over X characters)
- How can I display widget in a Page or Post?
- Ajax load more button in Recent posts widget
- I want to display custom text widgets for each post in the sidebar?
- remove post and categories/tags count from right now dashboard widget
- Widget that shows categories with posts numbers
- Getting Duplicate Posts on Home Page with Widget Added Between Posts
- Display current post position in Elementor Posts widget
- Custom posts listing widget always returns 1 extra result
- Need to know custom code to display random and most viewed posts in wordpress posts and pages
- Load widget only on blog posts
- Widget to show posts in the sidebar basing on its IDs
- How to show multiple posts thumbnail, title and date in widget
- Move position in post list for a custom checkbox column
- Add checkbox in admin post list – show checked post in widget
- Create a List of Widgets, where each widget holds three attributes
- Custom Fields – How to get the list of a specific active widget each time it rendered
- How do i bring out post category name in my widget?
- post__in no longer works in wordpress 5.0.3
- Posts on Sidebar only
- How do I create a ‘selection’ criteria list’ that will then dictate which posts are generated when the user presses search?
- Creating widget – ask for selecting a post in the admin panel
- How to display most popular posts from certain category in sidebar?
- Post List Widget with custom posts and editing the Read More Link
- Hide first blog post in content and output it in widget
- How to use in_category?
- How can to use the players from the Media Widgets in the body of a post to play audio and video files
- How to have more post in a page than in your home page
- How to add a widget area between blog posts in Genesis Framework?
- wordpress pinboard theme [closed]
- Output number of WordPress posts remaining until a post milestone is reached
- Image inside the content is replaced with featured image from my older post
- Adding additional data to WP_Post object
- How set featured posts using checkbox in post edit screen?
- using wp_update_post on save_post
- WordPress plugin to publish to multiple remote WordPress blogs
- How do you get all the urls of images attached to a post?
- Get names of authors who have edited a post
- Display posts by tag on page
- How to batch update post content with custom post meta value
- Custom wp_trim_words() function not trimming right
- How to create post in WP network using WP-CLI
- How do I add a reusable block to a programmatically created post?
- move blog posts from one wordpress to another
- The ‘https_local_ssl_verify’ filter
- How to run a function when post is edited or updated using publish post action?
- Automatic value for custom fields for posts
- Display number of posts via adress bar?
- Display most recent post on homepage?
- Category Template: Need to display different content on first page of archives
- Get image from post’s gallery
- get current post id inside hook
- Display all posts that were published before full post on single.php
- How to make number of blog posts a custom field?
- check if wordpress is updating or publishing a post
- Sort WP posts by span value
- Add Read More Tag to a post content (Single.php)
- How to compare two posts including their meta fields on a scalable base?
- Why does my site often display 404 on subpages?
- Move comments only from one WP installation to another
- How to change the color theme per post?
- How to add tags (custom taxonomy) to post class css?
- SQL Command for restoring trashed comments
- Loop don’t work within single.php page
- WordPress.com post editor replacement
- WordPress Menu options in the Admin: Posts for the dropdowns
- What is $post->pre_post_content exactly?
- How to split authors?
- Display Related Posts by Category in Random
- Showing posts for today and future
- Display 3 posts with different HTML markup using a loop
- Jumbled writing under a post
- Moved to a new server, backup was a day old and missing 3 posts, anyway to recover them from old host?
- Hook save_post_cpt not triggered when custom fields are modified
- 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?
- Get category slug of the Parent category of a Product
- How to limit wordpress the_excerpt() dynamically
- Reuse old post ID for new post after deleting post
- Contact Form 7 Shortcode not recognized inside another shortcode
- WordPress Pods Custom Post Type – separate Media Upload folder for each custom Post Types
- WP posts using index.php instead of single.php
- Excluding tag Link from html tags in Post content
- Add in the url attachment loop the wp_link_pages of the post
- Couldn’t view individual post that were in my website
- Get the category of post
- 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]
- Problem with jQuery in post body?
- Posts page template: How can I edit the markup for this?
- Pagination working locally but not on live site
- How can I display these posts in ascending order?
- Is there any point to using wp_unique_post_slug?
- How to monitor the first publish date?
- Twenty Twenty-Four: Static Front Page with Post Links