It’s not clear why you mess with the global, under normal circumstances you don’t need to.
Out of the box, when viewing a post page, get_post()
will return current post object. Regardless of the loop even, since it is set up as part of main query before template is even reached.
The difference between post and page for this purpose is minimal as well.
If get_post()
call alone return empty/unexpected results, then the likely issue is that something messes up global context before this point is reached.
Related Posts:
- Content hooks vs User hooks
- Restrict post edit/delete based on user ID and custom field
- Get updated post meta on save_post action?
- Post IDs missing on delete_postmeta action hook
- Set default Custom Post Meta Value
- wordpress is_front_page() issue
- Why does get_post() not return anything?
- Remove action of an external plugin after checking if custom post meta is set
- How can I get the post ID from a WP_Query loop?
- How to edit a post meta data in a Gutenberg Block?
- How to update/delete array in post meta value?
- Adding an assisting editor box to Post page
- How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display on a website?
- Get Advanced Custom Fields values before saving [closed]
- Why am I getting an infinite loop with have_posts?
- adding a URL to a post meta
- update_post_meta() not working when used with WordPress action
- Adding meta data to an attachment post
- Get posts by meta value with date
- Delete post meta conditionally after save post
- Problem With Order Item Meta In Woocommerce
- Job of meta_key meta_value fields in database tables
- How to store Gutenberg ColourPicker RGBA as metadata
- Is it possible to update a post meta field through REST API if the format of it when registered is nested?
- trying to do if post meta !=0
- Trying to get file name
- Best way to save postmeta
- How to add meta data to WordPress posts?
- How to update Post Meta values through the comment system
- Filtering multiple meta_values
- How add post_meta from user_meta?
- How can I get my iFrame to work with url from post_meta?
- Removing Post Meta from Category Pages?
- How to use update_post_meta() function properly?
- Hey, I want the second options. get_post_meta()
- populate post meta in gravity forms [closed]
- Plugin with action ‘save_post’ needs to press publish twice on order to publish
- How to display post view count by date, week and month?
- Get User Post if Private
- How to receive all the meta information of a post ?
- How do I dynamically call a page/post author into the meta data?
- Set Condition echo function get_post_meta
- Compare old meta with new post meta
- need to add add user names to the post for later retrieval or removal
- Read post meta values, only if posts are public
- Custom Post type forgetting data
- Like & Dislike functionality using JavaScript
- Time & Date on Post – Time Ago Custom Function
- Mass removing CSS from 1,700 post [closed]
- How to remove Date from Meta Description on SERP?
- Hide post title input for all roles except admin
- get meta value from page query
- Help with Post Meta
- why after saving meta value it’s saving all the values the one that i clicked?
- How do I manage custom meta in post revisions in the Block Editor era?
- Update post meta within save_post action
- Generate metadata for cloud images
- Migrating meta value to new meta value
- Insert multiple metas in one time
- How to extract data from a post meta
- count post meta values
- Correct meta of Writing Meta Tags
- Get post_meta from specific post [closed]
- How to add tags to post from frontend?
- How can I get some of a posts meta data whilst it is still inserting?
- Retrieve user_meta and copy to post_meta
- getting the post_id from the post_meta
- Displaying the ‘time ago’ for recent activity within a post/custom post type?
- add post meta front end edit
- How to query and update one colum in postmeta table?
- Post via wp-admin and via iOS app, same hooks and triggers involved?
- troubles with get_post_meta (and saving it)
- How to remove date from showing in meta description
- How to save HTML data into SQL post_content column
- update post meta value with other post meta value
- How to merge the array values in foreach?
- Negative meta_query if storing multiple post_meta values with shared meta_key
- How to exclude posts by meta key value in the_post_navigation next prev links?
- How to save a meta_value as a numeric value after I retrieve it via update_post_meta?
- update_post_meta not working with transition_comment_status
- Update post meta date always store 1970-01-07
- Is it possible to pass an whole array using custom field in wordpress?
- Attachment metadata has value of ‘1’
- How do I update a specific value within array in a products metadata?
- Optimize WP Meta Query for large amount of post meta?
- Programatically delete several attributes in _product_attributes in post meta
- rendering open graph meta tags in wordpress
- Get post meta after insert post
- Data not displaying in text field
- WP-API + JS Backbone client – how to update post meta
- Proper Way to Sanitize Meta Input
- update_post_meta returning false when called during init
- Getting meta content from get_next_post & get_previous_post
- Unsure how to add simple checkboxes that write to a small table to admin
- How to get pictures of the WordPress post
- Get value from post meta array, and add it as a category
- Access Serialized Post Meta Values
- Cache metadata for set of posts
- Problem with get_post_meta
- Post meta box data not saving