2014-02-26 45 views
0

這段代碼根據請求返回的是按ID排序的數據,而不是作者,所以我試圖想出另一種方法來處理它。get_posts orderby ='author'not working,looking for a alternative

$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通過姓氏進行搜索和排序?或者有關如何讓這個簡單的代碼工作的任何想法。

+0

嘗試使用author_name而不是作者? – Howli

+0

不,''orderby'=>'author'是正確的參數'。 – AndyWarren

回答

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; 
    } 
} 

讓我知道,如果它的工作原理;)

乾杯!

+0

謝謝Jeffrey ...但這段代碼只是輸出最後一個用戶 – user3105748

+0

編輯了答案。隨意嘗試。謝謝! –

+0

IT WORKED !!!!!!!!!!!!!!!非常感謝! – user3105748