It can be done like this without having to rely on the $post global.
if ( is_admin() ) {
if( isset( $_GET['post'] ) && isset( $_GET['action'] ) && $_GET['action'] === 'edit' ) {
$post_id = $_GET['post'];
$current_post = get_post($post_id);
$author_id = $current_post->post_author;
}
}
The query string variable post
is always the post_id
you are editing and additionally you need to check if the action
query variable is set and it equals to edit
.
Additionally you can explore the get_current_screen() function to setup more advanced conditionals for better security.
Related Posts:
- Get_the_author doesn’t return author name
- Unreadable pagination
- Posts missing on author page
- Setting $_SERVER[‘HTTPS’]=’on’ prevents access to wp-admin
- the_author_posts_link css class
- How do I get the author’s page url from their ID?
- Current post’s author name in the author meta tag
- Handling error states with admin_post
- Insert code when users come from an specific referer
- How to display related posts from parent category
- Missing Author Information
- Make custom field meta not display if there is not data in it
- Performance issues with large website [closed]
- How to migrate the posts from an old custom legacy blog to a new WordPress website?
- How to override wp-admin styling
- White Screen of Death – wp-admin
- How can I use wp_query to show all product data using just the products ID?
- Get Current User Id Inside a Loop Returns 0 For a Shortcode
- Trying to add attribute to my posts’ featured image
- Storing Array from returned database query and using the array in a new query
- How to show part of the_content?
- How to set a min number of words for a blog post
- Get post id in a function when edit/add a post
- Which hook/action will help me solve my problem?
- How to make my custom widget appear within WordPress widgets? Plugin development
- Post + form + action + results on the same page
- Programmatically create page when saving custom post type post
- WordPress admin is incredibly slow [closed]
- Loading index.php contents which located outside blog folder for post single page
- get post content of particular post by url
- Post repeating with infinite scroll
- Count posts on multisite with blog id
- Making (and edit existing) posts with ID included
- Parsing Menu Items and Blog Posts
- Pagination on Custom Post
- $wpdb returns duplicate posts
- How to show an entire post content and not also the excerpt?
- Usage of call back function of add_meta_box()
- Why is a wp function used in current PHP namespace’s callback not resolved to global scope?
- set post limit at load more ajax wordpress
- Increment paged on WP_Query
- Add new post in existing categories using wp_insert_post
- How do I add more options to the post-new.php page?
- Modify main query before it run without pre_get_post
- .html end of URl affected to SEO?
- Get the id of all images in a post
- Four posts per row [closed]
- Update if post with specific title exists
- Need help removing […] after excerpt
- I installed WordPress locally now how do I login?
- How to debug failed changes to posts?
- Display message depending on capability outside loop
- Pagination in single.php
- Advanced Query posts
- How can I update WordPress plugins or WordPress itself in all server?
- My Custom Post Type AJAX Query is Returning no posts – why?
- Search bar for wp menu
- How to get post image caption by index
- Inside Array – “unidentified index” error with “prepare was called incorrectly” despite not calling the prepared statement with wordpress [closed]
- Is there anyway I can call the year once?
- Check $post->ID against multiple values
- Class ‘WP_Privacy_Requests_Table’ not found
- Don’t update modified post date when user add a product review or comment?
- Limit the number of characters/words in an excerpt for a related posts section
- How to allow users to save list of posts in wordpress?
- How to separate the taxonomies list from the form so that editing taxonmies is the same as pages and posts?
- Closing Comments conditionally in comments.php
- How to center all text body in single.php at once?
- Having trouble with customizing blog posts on the home page
- spl_autoload_register is slow for WordPress sites
- Related Posts: if there is no category use tags
- How do if all posts has this category ID then do this
- Send email to users when they get new followers
- HELP! Frontend User Profile Edit Won’t Update Email
- Get current_post in reverse order with pagination
- How to make a parent page for author.php?
- defined (‘ABSPATH’) false after AJAX post to other PHP-file
- Getting error in sql query
- How do I get the featured post to share on Social Media and show on Individual Post pages?
- Insert div after every three posts in home.php [duplicate]
- PHP tag in post content makes wordpress go crazy
- Unable to get Post Category Name and URL
- Dynamically Create Posts Via XML File
- Basic PHP question (displaying post title over thumbnail) [closed]
- insert thumbnail image from php script
- Post from front end form to post_meta
- media_handle_sideload() returned post_id being incremented
- Saving Post redirects to 404 page when php $_GET variable in post body
- How can I add WordPress Audio Player as featured audio in Audio Blog Posts?
- Trying to see if page is category or single and displaying title with appropriate heading tag
- When I use rewind_post() it shows menu items in my loop
- How to customize posts in WordPress by using HTML and CSS?
- foreach all the post
- Blog Posts not sorted and pagination not adjusted after filtering some category
- What is the best way to reset the site? Different fails while set up
- How to create a WordPress PAGE in another folder?
- How do I get a function to work in single.php
- Add Woocommerce Customers link to custom admin menu
- Count the number of matching post names in foreach loop
- Need Help With Making Full-Width Template for Blog Posts (common methods aren’t working)