admin管理员组

文章数量:1122846

Im looking to get user information by email address using the latest version of the wordpress restapi.
I have registered the user email field and it returns on both the "/wp-json/wp/v2/users/" and the "/wp-json/wp/v2/users/" calls but im looking for a way to basically get user by email. I cant seem to find a way to filter or search by email field though, any thoughts?

Im looking to get user information by email address using the latest version of the wordpress restapi.
I have registered the user email field and it returns on both the "/wp-json/wp/v2/users/" and the "/wp-json/wp/v2/users/" calls but im looking for a way to basically get user by email. I cant seem to find a way to filter or search by email field though, any thoughts?

Share Improve this question edited Apr 26, 2018 at 17:00 mmm 3,7933 gold badges16 silver badges22 bronze badges asked Apr 26, 2018 at 15:43 KoleonKoleon 412 silver badges5 bronze badges 2
  • if the core endpoints don't help you, you can create a new one : developer.wordpress.org/rest-api/extending-the-rest-api/… – mmm Commented Apr 26, 2018 at 17:04
  • Maybe you take a look here (found by using google) wp-api sample and grab the code (and correct deprecated stuff) you need?! – Charles Commented Apr 26, 2018 at 17:54
Add a comment  | 

3 Answers 3

Reset to default 6

You can use the search argument

https://example.com/wp-json/wp/v2/users/[email protected]

A tad late to the party, but the trick is to make sure your search query is url-escaped (i.e. "%40" instead of "@").

The following fetch call worked on my site:

https://example.com/wp-json/wp/v2/users/?search=myemail%40domain.com

Or for a more friendly input experience (that doesn't require escaping special characters) I created this function:

function get_user_by_email(email) {
    return fetch( `https://example.com/wp-json/wp/v2/users/?${new URLSearchParams({ search: email })}` )
        .then(response => response.json());
}

get_user_by_email("[email protected]")
    .then(console.log);

You need to add the database field user_email to the search columns for user REST API query. Therefore you can use the filter rest_user_query.

add_filter( 'rest_user_query', function ( array $prepared_args ) {
    $prepared_args['search_columns'][] = 'user_email';

    return $prepared_args;
} );

Now it works, if you search the email like: https://example.com/wp-json/wp/v2/users/[email protected]

本文标签: WP Rest API Get User by Email