I'd like to call WP REST API to update a user. I have only username, so I suppose I should retrieve user ID first of all, as the endpoint for updating a user is:

GET /wp/v2/users/<id>

How to retrieve the ID with username (that is unique as well)?

I tried:

GET /wp/v2/users/?username=USERNAME 

but it's not working.

I also tried:

GET /wp/v2/users/?slug=USERNAME

and it works, but I'm not sure it would not return duplicates.

The best would be update directly the user with a single call, with the username as key, but if this is not possible, I would like to be sure 100% the retrieved ID is the one of the unique username I provided.

  • I think there's a bigger architectural problem here that needs to be solved. The question I would ask is: Why do you only have the username? If you need to update users over the API, then you need the ID, so I would suggest reworking your application so that you always have the ID available so that you don't need to make extra requests. – Jacob Peattie Aug 7 '19 at 7:26
  • Is either what @JacobPeattie says or create your own endpoint I guess. – Luis Rivera Aug 7 '19 at 7:41
  • what about the first request: how to retrieve the ID given the username without ambiguity? – jasmines Aug 7 '19 at 8:29
  • With the built-in endpoints, you can't. The users/?slug= option is the closest. – Jacob Peattie Aug 7 '19 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.