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.
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.
Share Improve this question edited Feb 15, 2021 at 13:05 Álvaro Franz asked Feb 15, 2021 at 12:58 Álvaro FranzÁlvaro Franz 1,1001 gold badge9 silver badges31 bronze badges 2 |1 Answer
Reset to default 2Thanks 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,
....
"author"
, but only when the user has the admin role? – Tom J Nowell ♦ Commented Feb 15, 2021 at 13:28