最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Assign a Custom Role to a Custom Post?

programmeradmin1浏览0评论

I been looking around for this, but couldn't find a way. The scenario is: I have a custom post type 'Company', and I'm creating Company type posts through my plugin's code. When I create a new Company post, I set the author as Admin. Now further down the code, I'm creating a custom role 'Representative' and assigning users to this role through my code again. What I want to do is, assign a Representative to a Company Post as an author. But I can't even see this user(Rep.) in the dropdown for switching authors. Is there any argument that I need to pass to wp_insert_user or register_post_type or add_role/add_cap for achieving this?

Thanks in advance!

I been looking around for this, but couldn't find a way. The scenario is: I have a custom post type 'Company', and I'm creating Company type posts through my plugin's code. When I create a new Company post, I set the author as Admin. Now further down the code, I'm creating a custom role 'Representative' and assigning users to this role through my code again. What I want to do is, assign a Representative to a Company Post as an author. But I can't even see this user(Rep.) in the dropdown for switching authors. Is there any argument that I need to pass to wp_insert_user or register_post_type or add_role/add_cap for achieving this?

Thanks in advance!

Share Improve this question asked Aug 9, 2011 at 6:19 Rutwick GangurdeRutwick Gangurde 8,6045 gold badges42 silver badges55 bronze badges 6
  • Would be very interested in knowing the answer to this as well. Good luck. Sorry I couldn't be any help. – Dwayne Charrington Commented Aug 9, 2011 at 6:32
  • NP Dwayne! I'll post it if/when I find it! – Rutwick Gangurde Commented Aug 9, 2011 at 6:39
  • When I need to add new role/cap I use Justin Tadlock's Members plugin it saves a lot of time - wordpress/extend/plugins/members try it – Mamaduka Commented Aug 9, 2011 at 7:32
  • @Dwayne - I hacked it and it works fine! You interested in knowing how? Mamaduka - Thanks for the suggestion, but I'm doing a lotsa custom stuff with roles/caps/customs, and Members plugin wouldn't allow me the customization I needed. – Rutwick Gangurde Commented Aug 9, 2011 at 8:04
  • try this plugin wordpress/extend/plugins/map-cap – Bainternet Commented Aug 9, 2011 at 10:21
 |  Show 1 more comment

3 Answers 3

Reset to default 5

I'm reposting this with an example, I really don't get the code formatting of this editor, last time I was so irritated with it that I skipped the code!
That dropdown is populated using a filter 'wp_dropdown_users' (user.php, line 976). This filter returns the dropdown (html select) as a string. You can intercept this string, and add your own options, which is the list of users with the custom role. Inspect the select with Firebug, the option value is the user id, and text is the login name for that user.

<?php 
    add_filter('wp_dropdown_users', 'test');
    function test($output) 
    {
        global $post;

        //Doing it only for the custom post type
        if($post->post_type == 'my_custom_post')
        {
            $users = get_users(array('role'=>'my_custom_role'));
           //We're forming a new select with our values, you can add an option 
           //with value 1, and text as 'admin' if you want the admin to be listed as well, 
           //optionally you can use a simple string replace trick to insert your options, 
           //if you don't want to override the defaults
           $output .= "<select id='post_author_override' name='post_author_override' class=''>";
        foreach($users as $user)
        {
            $output .= "<option value='".$user->id."'>".$user->user_login."</option>";
        }
        $output .= "</select>";
     }
     return $output;
    }
?>

That's it! You'll have the dropdown listing your custom roles. Try changing the post author, it gets updated neatly. It's a hack, but it worked for me!

Update of the previous posted function that duplicate the selectbox and not display authors name. Also added support for multirole :

add_filter('wp_dropdown_users', 'addAuthorsToSelect');
function addAuthorsToSelect($output)
    {
        global $post;
        if($post->post_type == 'my_custom_post_type')
        {
            $users = get_users(array('role__in'=>array('role1','role2','role3','role4')));
            $output = "<select id='post_author_override' name='post_author_override' class=''>";
            foreach($users as $user)
            {
                $output .= "<option value='".$user->id."' ".(($post->post_author==$user->id)?'selected="selected"':'').">".$user->display_name." (".$user->user_login.")</option>";
            }
            $output .= "</select>";
        }
        return $output;
    }

The above solutions work only with the WordPress classic editor but not with Gutenberg since WordPress 5.0. The new block editor populates the author dropdown with an AJAX call using the REST API. Taking a look at the network tab on the console you can see a call /wp-json/wp/v2/users/?who=authors&per_page=100&_locale=user to read the authors.

Working solution for me was modifying the user query args using the rest_user_query hook.

add_filter('rest_user_query', 'add_custom_role_to_authors', 10, 2);

function add_custom_role_to_authors($prepared_args, $request ) {
    $prepared_args['who'] = array('authors','custom_role');
    return $prepared_args;
}
发布评论

评论列表(0)

  1. 暂无评论