• Resolved edosauyi

    (@edosauyi)


    Please I need help.
    The code below will search for users with “jo*” in their username or email and display their usernames. The issue now is that I don’t want to search any of those fields, i want it to ONLY search for users with “jo*” in their first_name and display their usernames.

    I understand it can be done with get_userdata function, someone should please help me with the right code to do it.

    <?php
    
    $blogusers = get_users( array( 'search' => 'jo*' ) );
    
    foreach ( $blogusers as $user ) {
    	echo '<li>' . esc_html( $user->user_login ) . '</li>';
    }
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I got the impression from your previous topic that you were not interested in coding your own solution. Since I was apparently wrong, something like this should do it:

    $blogusers = get_users( array(
       'meta_key' => 'first_name',
       'meta_value' => 'jo%',
       'meta_compare' => 'LIKE',
    ) );
    // foreach loop etc...

    get_userdata() only works if you already know the user ID you want data for. To get the ID you still need get_users(). Then the users returned already have the userdata within the returned user objects, so there is not much need for get_userdata() here.

    FYI, in SQL the wildcard character is ‘%’, not the usual ‘*’, but only in context with the LIKE or NOT LIKE operators. Wildcards are not valid with other operators.

    Thread Starter edosauyi

    (@edosauyi)

    Thank you very much sir, but there’s still a little problem

    I arrived at this code (i’m trying to tune it to fit into what i need it for)

    $blogusers = get_users( array(
       'meta_key' => 'first_name',
       'meta_value' => jo%,
       'meta_compare' => 'LIKE',
       'role' => 'money',
       'orderby' => 'registered',
       'order' => 'DESC',
       'number' => '',
    ) );
    
    if ( ! empty( $blogusers ) ) {
    
    foreach ( $blogusers as $user ) {
    
    	echo '<tbody><tr><td><font color=green><i><b>' . esc_html( $user->user_login ) . '</b></i></font></td></tr></tbody>';
    
    }
    } else {
    	echo 'You Currently Have.';
    }

    Please how/where can i add the open/close html code for table?

    And, can i add pagination? I would like to display 50 users per page

    Thread Starter edosauyi

    (@edosauyi)

    My code looks funny to me but it solved the table aspect, check it below

    $blogusers = get_users( array(
       'meta_key' => 'first_name',
       'meta_value' => 'jo%',
       'meta_compare' => 'LIKE',
       'role' => 'money',
       'orderby' => 'registered',
       'order' => 'DESC',
       'number' => '',
    ) );
    
    echo '<table>';
    
    if ( ! empty( $blogusers ) ) {
    
    foreach ( $blogusers as $user ) {
    
    	echo '<tbody><tr><td><font color=green><i><b>' . esc_html( $user->user_login ) . '</b></i></font></td></tr></tbody>';
    
    }
    } else {
    	echo 'You Currently Have.';
    }
    
    echo '</table>';

    Now, can i add pagination?

    Moderator bcworkz

    (@bcworkz)

    Sure, you can manage which users appear with the ‘offset’ and ‘number’ arguments in the get_users() array. You also need to manage what the currently requested page number is. One way to do this is to include the requested page number as an URL parameter.

    Let’s say the current page is 3. Then next page link might have an href like this: example.com/user_list/?user-page=4
    And the previous page link would be like this: example.com/user_list/?user-page=2

    Then the get_users() arguments would be like this:

    $blogusers = get_users( array(
       'meta_key' => 'first_name',
       'meta_value' => 'jo%',
       'meta_compare' => 'LIKE',
       'role' => 'money',
       'orderby' => 'registered',
       'order' => 'DESC',
       'number' => 50,
       'offset' => ($_GET['user-page']-1)*50,
    ) );

    Some special cases still need to be addressed, like if $_GET['user-page'] is not set, then we need 'offset' => 0,

    If the current page is 1, do not output a previous page link

    Determine the total number of users and calculate the number of pages needed to list all users. If the current page is the last page, do not output a next page link. You can count the users returned when using get_users() with 'number' => '', and no offset like your current code does.

    It’s kind of a shame (and inefficient) to get all user objects just to get a count. An alternative would be to make a count query using raw SQL through a $wpdb method.

    Thread Starter edosauyi

    (@edosauyi)

    Okay, thank you very much for the explanations. I’m very grateful for the support/assistance I always get on this forum all thanks to BCWORKZ. You’re the best!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Frontend Username Display By First Name Search’ is closed to new replies.