I need to take the ID of the post that is loaded in the dashboard for editing. get_the_ID
or get_queried_object()
seems to not work, even if this functions are supposed to be used also outside the loop.
I need the post ID to prevent a role from editing this post in the dashboard, so I can do something like: if the post that the user attempt to load has the ID == 9, redirect the user to the dashboard home or die.
So, how can I get the ID of the current post that the user attempt to load?
Edit:
The following is one of the piece of code I tried. If the role is matched, the user is in the dashboard and the post has the category ID == 20 (assuming that has_category
take the current post if an ID is not specified), then die.
add_action('admin_init', 'bck_restrict_post');
function bck_restrict_post() {
if (current_user_can('myrole') && is_admin() && has_category(20) ) {
wp_die();
}
}
It's very simple, but has_category
doesn't work. It doesn't take the current post ID and nothing change. I've also tried using in_category()
; same result.