I would like to set up a Custom Post Type which supports the author feature only for admins.
Other user roles may as well create, see and edit their own posts, but the admins should be able to switch a post’s author.
So far I managed to display only their own posts to non-admins thanks to the pre_get_posts filter.
And this is how I define the CPT. What can I do now to restrict the supports author only to admin users?
// CPT
function prefix_setup_cpt(){
register_post_type('whatever',
array(
'labels' => array(
'name' => 'Whatever',
'singular_name' => 'Whatever'
),
'supports' => array(
'title',
'editor',
'author' // <--- This enables author feature
)
....
I am not seeking for a coded answer to copy/paste, but for hints about hooks and other WordPress facts that help me achieve the desired functionality.
Thanks a lot for your attention.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Thanks to TomJNowell‘s help, this would be a possible working solution:
$supports = array(
'title',
'editor'
);
if(current_user_can('delete_plugins')){
$supports[] = 'author';
}
register_post_type('whatever',
array(
'labels' => array(
'name' => 'Whatever',
'singular_name' => 'Whatever'
),
'supports' => $supports,
....
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0