• dhiraj251082

    (@dhiraj251082)


    if(email_exists( $email )){
    
          echo 'Email exits: ' .  $email;
    
          $args2 = array(
    
            'user_email' => $email,
    
          );
    
      $query = new  WP_User_Query($args2);
    
        if ( !empty( $query->results ) ) {
    
          foreach ( $query->results as $user2 ){
    
          $user_id = $user2->ID;
    
          echo 'User ID: ' . $user_id;
    
           } }
    
           else {
    
          echo 'User not found';
    
      }
    
      }

    the above code is echoing all the id in the wp_users table

Viewing 1 replies (of 1 total)
  • aatanasov

    (@aatanasov)

    Hi, you’ll need to change the arguments of the $args2 array to:

    'search'         => $email,
    'search_columns' => array( 'user_email' )

    That way, you’ll receive the correct user.

    However, if you need to fetch the data only for a single user, it is better to use the get_user_by function. For example:

    $user = get_user_by( 'email', $email );

    It will create a WP_User object with all the necessary information for the particular user.

Viewing 1 replies (of 1 total)
  • The topic ‘wp_user_query not working fine’ is closed to new replies.