按姓氏对特定角色的用户进行排序

时间:2013-02-14 作者: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 {

    }
    ?>

2 个回复
最合适的回答,由SO网友:Sunyatasattva 整理而成

这件事显然有公开的证据bug.

以下是我测试的解决方法:

$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();
问题是它会运行两次查询。

SO网友:birgire

以下内容适用于我的安装:

$args  = array(
        \'orderby\'   => \'meta_value\',
        \'order\'     => \'DESC\',
        \'meta_key\'  => \'last_name\',
        \'role\'      => \'subscriber\'
    );
那么您是否尝试过:

$args  = array(
        \'orderby\'   => \'meta_value\',
        \'order\'     => \'DESC\',
        \'meta_key\'  => \'last_name\',
        \'role\'      => \'guest-teacher\'
    );
并更改order 参数到DESCASC 检查是否有任何影响?

附言:您是否为这些用户填写了姓氏?

结束

相关推荐