Skip to content

Instantly share code, notes, and snippets.

@renventura
Created August 28, 2020 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renventura/95ef0efc99c42092aa98180aa7677ef6 to your computer and use it in GitHub Desktop.
Save renventura/95ef0efc99c42092aa98180aa7677ef6 to your computer and use it in GitHub Desktop.
Modifies the "authors" included in the REST response for /wp/v2/users/?who=authors
<?php
add_filter( 'rest_user_query', 'my_custom_rest_authors', 25, 2 );
/**
* Modifies the "authors" included in the REST response for /wp/v2/users/?who=authors
*
* @param array $args Query args
* @param object $request REST request
*
* @return array
*/
function my_custom_rest_authors( $args, $request ) {
if ( ! empty( $request['who'] ) && 'authors' === $request['who'] ) {
// Customize which roles are considered authors
$rest_author_roles = array();
// Get registered roles
$roles = wp_roles();
// Check each role for the edit_posts capability
// Change this as needed
foreach ( $roles->roles as $key => $r ) {
$role = $roles->get_role( $key );
if ( $role->has_cap( 'edit_posts' ) ) {
$rest_author_roles[] = $key;
}
}
$args['role__in'] = array_merge( ! empty( $args['role__in'] ) ? $args['role__in'] : array(), $rest_author_roles );
$args['who'] = '';
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment