0

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.

2
  • 1
    So you want that array to contain "author", but only when the user has the admin role?
    – Tom J Nowell
    Feb 15 '21 at 13:28
  • 1
    @TomJNowell How did I not see it that way? Thanks a lot for your help sir! Feb 15 '21 at 14:17
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,
....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.