get_posts orderby ='author'不工作,寻找替代方案

时间:2014-02-26 22:25:32

标签: wordpress

这段代码返回按ID排序的数据,而不是请求的作者,所以我试图想出另一种方法来处理它。

$args = array('posts_per_page' => '-1','order' => 'ASC', 'orderby' => 'author');
$cat_posts = get_posts($args);

foreach ($cat_posts as $cat_post) :
    if (!in_array($cat_post->post_author,$author_array)) {
        $author_array[] = $cat_post->post_author;
    }
endforeach;

foreach ($author_array as $author) :
    $auth = get_userdata($author)->display_name;
    $nicename = get_userdata($author)->user_nicename;
    $option = '<option value="/author/'.$nicename.'">';
    $option .= $auth;
    $option .= '</option>';
    echo $option;
endforeach;

有没有办法使用usermeta = user_lastname通过last_name进行搜索和排序?或者有关如何使这个简单代码起作用的任何想法。

1 个答案:

答案 0 :(得分:0)

随意尝试:

$args = array('posts_per_page' => '-1','order' => 'ASC', 'orderby' => 'author');
$cat_posts = get_posts($args);
$options = array();

foreach ($cat_posts as $cat_post) :
    if (!in_array($cat_post->post_author,$author_array)) {
        $author_array[] = $cat_post->post_author;
    }
endforeach;

foreach ($author_array as $author) :
    $auth = get_userdata($author)->display_name;
    $nicename = get_userdata($author)->user_nicename;
    $lastname = get_userdata($author)->last_name;
    $options[ $lastname ][] = '<option value="/author/'.$nicename.'">' . $auth . '</option>';
endforeach;
ksort($options);
foreach ($options as $key => $option) {
    foreach( $option as $k => $v ){
      echo $v;
    }
}

让我知道它是否有效;)

干杯!