• Resolved instanceid

    (@instanceid)


    I am working on using the API to search for a user by email and then getting the ID of the user and then executing a change to the account based on the ID. The only issue is, it seems that when I search, the search doesn’t require it to be exact. I am wanting it to be exact.

    Example is, I search for a user using:

    "GET /wp-json/wp/v2/[email protected] HTTP/1.1" 200
    It comes back with the correct user, but if I also tried:

    "GET /wp-json/wp/v2/users?search=username@gmai HTTP/1.1" 200
    That also came back with the user. I am developing just using a test site so I only have a few users, but I foresee that this could be a problem. I am needing to search for one specific user by email at a time and I was hoping that when I got a result back I could validate it by checking if the email address shown in the json search result matched the email that was passed to it, but it doesn’t look like it displays the email address in the results either.

    Does anyone have any suggestions on what I can do to get things a bit closer to what I am after?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Dion

    (@diondesigns)

    My suggestion is to use database queries and not the REST API. Not only would you obtain exactly what you want, it would be much easier on a site.

    Thread Starter instanceid

    (@instanceid)

    Hey there,
    I appreciate the reply. I got it worked out already. After a bunch of research I was able to make this and it works well.

    add_action( 'rest_api_init', function () {
        register_rest_route( 'endpoint/v1', 'email/(?P<stringvar>[^/]+)', array(
            'methods'             => 'GET',
            'callback'            => 'user_email',
            'permission_callback' => function () {
                return current_user_can('edit_others_posts');
            },
        ) );
    });
    
    function user_email($data) {
    
        // Get user by their email address
        $user = get_user_by( 'email', $data['stringvar']);
        $userId = $user->ID;
        $user_data = [$userId, $data['stringvar']];
    
        wp_reset_postdata();
    
        return rest_ensure_response($user_data);
    }
    Thread Starter instanceid

    (@instanceid)

    Closing this.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Search via api for exact match only?’ is closed to new replies.