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
- How to change the case of all post titles to “Title Case”
- Hook that get’s triggered when the author of a post is changed
- Encryption of WordPress Posts
- How to exclude latest x posts from a paginated query?
- Keep a fluid max number of posts on site
- multicheck box for post metabox
- Have save_post write to database image meta [closed]
- WordPress get posts by date without query_posts
- Pagination not working on custom query
- Testing for post title in ‘if/else’ statement returns no content
- Number Unread comments/posts
- Importing New Posts from Live Site into Local Dev Site
- PHP Notice error (when on 404 page)
- Add container div to gallery using post_gallery
- WordPress’ visual editor messing up my (nested) lists (and other things as well)
- Create dropdown list post
- Show featured image next to post-teasers in Genesis Framework?
- I would like to give special promotion for the first 100 posts in my blog? Can anyone tell me how to do that?
- Make view count for pop up post
- add_rewrite_rules for type of post
- Trigger a Link once a new post is Published
- Edit custom post type admin menu link
- get_the_content(“more…”) returns full text
- How to load a post into an empty div tag anywhere across the pages?
- How to display Common posts from specific Tag & Category with Shortcode
- How can I get the media inside a post?
- Permission issue with custom post type – not added to menu – by plugin
- Get Page ID of blog homepage
- Order posts alphabetically: how to set order=asc in mysql query?
- How to order posts in an arbitrary order?
- Trying to alter the post_content through the_post
- How to get ID of the page included with get_page()?
- save_post hook is not called when post is saved
- Howto show comment count by post in post?
- How to add a class to each individual post?
- Summary of Posts by an User
- ACF to select posts not displaying on blog page
- Check if current user has a post and that post has any term/s from a specific custom taxonomy outside the loop
- How to remove “» (title of post or page)”?
- Extract wordpress posts content and category content
- WordPress loop not working on static front page
- Multiple Blog Pages
- Closest post to a date
- Adding if statement into the_content()
- Change modified date to current date when title updated automatically
- how to find the posts page
- Showing Posts if non are found
- how to Update guid = “text”+post_name?
- SQL DELETE multiple post_meta on single and multi-sites
- How to Best Create Separate Subscribers and User Experience
- Post not using taxonomy template
- Diffrent layout for posts [duplicate]
- query posts with selected post ids first
- How to display related post from same category in single.php
- Save All Post Permalink From A Specific Category into a .txt file
- How to get value of a selected option from select tag and use it in WP_query to filter posts?
- Using WP page password protection as defacto login
- get current index of post content in category page
- Permalink structure for two seperate blogs
- wp_query args with relation
- How do I apply a wordpress function to something with no ID inside an archive page?
- Add new post only in assigned category
- How to retrieve custom field values and save again as array
- include other content type post ID not working
- How to remove a date from wordpress post