Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to display a list of authors by their last name. I can get the list to display but as yet I don't seem to be able to order the list by last name. Any help would be greatly appreciated. Josh

<?php
    $args  = array(
    // search only for Authors role
    // order results by display_name
    'orderby' => 'meta_value',
    'meta_key ' => 'last_name',
    'role' => 'guest-teacher'



    // check for two meta_values
    );
    // Create the WP_User_Query object
    $wp_user_query = new WP_User_Query($args);
    // Get the results
    $authors = $wp_user_query->get_results();
    // Check for results
    if (!empty($authors))
    {
        echo '<ul class="permanent">';
        // loop trough each author
        foreach ($authors as $author)
        {
            // get all the user's data
            $author_info = get_userdata($author->ID);
            $url = get_author_posts_url($author->ID);  

    ?>


        <h3><a href="<?php echo $url; ?>"><?php echo $author_info->last_name; ?>         </a></h3>

            <?php
        }
        echo '</ul>';
    } else {

    }
    ?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

There is apparently an open ticket about this bug.

Here is a workaround that I tested:

$args  = array(
    'meta_key' => 'last_name',
    'role'     => 'guest-teacher'
);

$wp_user_query = new WP_User_Query($args);
$wp_user_query->query_orderby = str_replace( 'user_login', 'wp_usermeta.meta_value', $wp_user_query->query_orderby );
$wp_user_query->query();

$authors = $wp_user_query->get_results();

Problem with this is that it runs the query twice.

share|improve this answer
    
Thanks. Unfortunately I get no results with your code. When I remove the second query I get the same results in order of ID. –  Josh Feb 14 '13 at 14:50
    
That is weird, I just tested and it works for me. Mind that I edited the answer with an adjustment. Try again, and also tell me what happens if you var_dump($authors); –  Sunyatasattva Feb 14 '13 at 15:03
    
I get a NULL back. –  Josh Feb 14 '13 at 15:06
    
Can you var_dump($wp_user_query->query_orderby) both before changing it and after? –  Sunyatasattva Feb 14 '13 at 15:15
    
Before I get string(23) "ORDER BY user_login ASC" After I get string(35) "ORDER BY wp_usermeta.meta_value ASC" –  Josh Feb 14 '13 at 15:24

The following works on my install:

$args  = array(
        'orderby'   => 'meta_value',
        'order'     => 'DESC',
        'meta_key'  => 'last_name',
        'role'      => 'subscriber'
    );

So did you try:

$args  = array(
        'orderby'   => 'meta_value',
        'order'     => 'DESC',
        'meta_key'  => 'last_name',
        'role'      => 'guest-teacher'
    );

and change the order parameter to DESC or ASC to check if it has any effect?

ps: And do you have the lastname filled out for these users?

share|improve this answer
    
thanks, but it is having no effect regardless of what I put in the meta_key parameter. It seems to keep displaying in order of user ID. –  Josh Feb 14 '13 at 14:12
    
ahh, same thing happening to me, by change the ids are in the same order as lastname ;-) Will check if I can order by lastname. –  birgire Feb 14 '13 at 14:17
    
This should be the best answer ! –  Warface 9 hours ago
    
@Warface thanks, glad to hear it worked out for you. –  birgire 9 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.