6923

WordPress Filter for changing author list in edit post Authors box

Question:

I want to change the list of users in the Author select dropdown on the edit post page for a custom post type. Is there a filter hook I can use for this? I haven't been able to find any information on a filter hook that does what I want.

The hook should (in theory) let me return a user array and those will be the users that populate the select box at the bottom. The reason I want to do this is so I can conditionally filter out users by their role for different post types. As an admin (or other admins) I don't want to have to check if user has certain role before making them the author.

Example of code:

add_filter('example_filter', 'my_custom_function'); function my_custom_function ( $users ){ // Get users with role 'my_role' for post type 'my_post_type' if( 'my_post_type' == get_post_type() ){ $users = get_users( ['role' => 'my_role'] ); } // Get users with role 'other_role' for post type 'other_post_type' if( 'other_post_type' == get_post_type() ){ $users = get_users( ['role' => 'other_role'] ); } return $users; }

Answer1:

You can use hook 'wp_dropdown_users_args'.

Add below code snippet in your theme's functions.php file.

add_filter( 'wp_dropdown_users_args', 'change_user_dropdown', 10, 2 ); function change_user_dropdown( $query_args, $r ){ // get screen object $screen = get_current_screen(); // list users whose role is e.g. 'Editor' for 'post' post type if( $screen->post_type == 'post' ): $query_args['role'] = array('Editor'); // unset default role unset( $query_args['who'] ); endif; // list users whose role is e.g. 'Administrator' for 'page' post type if( $screen->post_type == 'page' ): $query_args['role'] = array('Administrator'); // unset default role unset( $query_args['who'] ); endif; return $query_args; }

let me know if this works for you or not.

Recommend

  • MVC3 - Conditionally add id element with Razor
  • Can I put condition in emacs lisp macro?
  • Application Express: Anonymous PL/SQL Block and Bind Variables
  • When querying against a view, a filtering clause in the view's definition is being ignored
  • Creating UDF with VSTO in Excel
  • How to set an entity field that does not exist on the table but does exists in the raw SQL as an ali
  • how to query for min or max inet/cidr with postgres
  • Enumerating Controls on a Form
  • sweetalert2 inputoptions from file in select example
  • uml Composition relationships to RDF and OWL
  • order post according to custom array position
  • Azure table store snapshot/backup capability
  • Trying to get the char code of ENTER key
  • Using Sax parsing to edit and write XML in VB6
  • Ajax calls do not work in IE unless you fiddle with security settings
  • xtable package: Skipping some rows in the output
  • Debug.DrawLine not showing in the GameView
  • Keep this build forever option - Jenkins
  • Yii2: Config params vs. const/define
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Avoid links criss cross / overlap in d3.js using force layout
  • Recording logins for password protected directories
  • Change JButton Shape while respecting Look And Feel
  • Splitting given String into two variables - php
  • What is the “return” in scheme?
  • Check if a string to interpolate provides expected placeholders
  • How to show dropdown in excel using jrxml (jasper api)?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?