0
votes

I want to list my users which are in wordpress according to their roles with json api.

Normally I use v2 api. x.com/wp-json/wp/v2/users?page=1&per_page=100

api doesn't return me user roles or I couldn't filter it. Like author or subscriber.

I wanted to write a different php file myself, where I can take the roles and read them, but the shortcoming here is that the user avatar does not come.

Here my custom php kodu.


$sql =  "SELECT u.id,u.display_name,um.meta_value  FROM wpuy_usermeta um inner JOIN wpuy_users u on u.ID = um.user_id WHERE meta_key = 'wpuy_capabilities' Order by u.display_name asc";


$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
       
    $rows[] = $row;
  }
  print json_encode($rows);

} else {
  echo "0 results";
}
$conn->close();

all I need is id user name, user role, user avatar. how can i solve this

1

1 Answers

1
votes

WordPress REST API is capable of giving you users roles.

However, in order to access this information, you need to declare the "context" structure as "edit".

Example Request URL

http://localhost/wp-json/wp/v2/users?page=1&per_page=100&context=edit

Here is the result of my own query with Postman in the image below.

enter image description here

But the result of doing this query directly will cause unauthorized access with 401 status code.

For this, you must verify your identity on the REST API or have successfully passed the authorization process with the new feature.

See also: WordPress Authentication, WordPress Application Passwords

Finally, the /users/ query already gives you the Avatar URLs.

enter image description here