• Resolved andy_woz

    (@andy_woz)


    I have a select menu for a search function populated by a custom query ordered by user last name that is working fine but I have a clunky exclude to stop the admin user being listed:

    <select name="author" id="select-author">
    <option value="">Select Author</option>
    
    <?php
    $excluded = "1"; //exclude users by ID
    $authors = $wpdb->get_results("SELECT * FROM $wpdb->users INNER JOIN $wpdb->usermeta ON ($wpdb->users.ID = $wpdb->usermeta.user_id) WHERE $wpdb->usermeta.meta_key = 'last_name' AND $wpdb->usermeta.user_id NOT IN ($excluded) ORDER BY $wpdb->usermeta.meta_value ASC");
    
    foreach($authors as $author):
    $select .='<option value="'. $author -> user_nicename .'">' . $author -> display_name . '</option>';
    endforeach;
    print $select;
    ?>
    
    </select>

    At some point I’m sure we are going to have more users and I don’t want to be excluding them based on their ID but I can make do by limiting the users to all those with the author role. I don’t see user_role currently listed in the resulting array for $authors. What I’d like to do is to add to the query and instead of excluding user ID’s have only users with the role of author in the results. What’s the best way to do this? I’ve tried a few things but nothing works so far……

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Sunny Johal

    (@sunny_johal)

    Hi Andy,
    Instead of querying the $wpdb global to get users I would just create a new WP_User_Query. So for your example I would do something like this (tweak as necessary):

    <?php
        // Parameters
        $query_args = array(
            'role'    => 'Author', // only include author user roles
            'exclude' => array( '1' ), // optional, array of ids to exclude
            'orderby' => 'user_nicename',
            'order'   => ASC,
            'fields'  => 'all',
        );
    
       $authors = new WP_User_Query( $query_args );
    ?>
    
    <select name="author" id="select-author">
        <option value="">Select Author</option>
        <?php foreach( $authors as $author ) : ?>
            <option value="<?php echo $author['user_nicename']; ?>"><?php echo $author['display_name']; ?></option>
        <?php endforeach; ?>
    </select>

    Also, if you get stuck here is a great tool to help you generate the user query that you need.

    Hope that helps!
    Sunny

    Thread Starter andy_woz

    (@andy_woz)

    Hi Sunny, I thought of something like this but I need them ordered by Second Name and as far as I can tell that’s not supported as one of the parameters. That seems to be the sticking point…… any ideas?

    Thanks for the above.

    Sunny Johal

    (@sunny_johal)

    Hi Andy,

    Second name is actually a meta value so the fields value in the $query_args array needs to be changed to ‘all_with_meta’. This will return an array of WP_User objects which would need to be sorted by the last name meta value. Before I can help you I need to know the following: What happens if no last name is entered?

    Sunny

    Thread Starter andy_woz

    (@andy_woz)

    Hi Sunny, if no last name is entered then I think ordering by either first name or by nicename would be a reasonable fallback not that there would be much different if nicename hasn’t been edited to something other than the WP default. Does that make sense?

    Thread Starter andy_woz

    (@andy_woz)

    Thread Starter andy_woz

    (@andy_woz)

    Closing

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Query – List Users by last name’ is closed to new replies.