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’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.
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 :
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.