2010-05-13 177 views

回答

0

query_posts()函數僅用於支持一次查詢一個作者。如果您想顯示多個作者,建議嘗試以下方法之一:

  1. 創建您自己的自定義查詢以包含多個作者ID。您可以使用$ wpdb-> query()來運行數據庫中的任何SQL語句,因此您可以肯定地獲得多個作者的所有帖子的列表,這隻需要更多的工作。
  2. 第二個選項是按作者分組您的帖子,併爲每個作者ID運行單獨的WordPress循環。這有點打破了你的內容,但你可以用一個簡短的作者生物序言爲每個部分添加序言。在this site上有示例代碼。
+0

謝謝,這完美滿足我的需求。簡單的解決方案#2。 – matchbranding 2010-05-14 03:09:38

+0

從頭開始,它不太合適。該頁面現在正在掛起。 – matchbranding 2010-05-14 11:44:17

+0

你能發佈一段代碼嗎?沒有理由多個循環解決方案應該掛起頁面。 – EAMann 2010-05-14 14:34:10

2

根據此文檔,沒有官方支持:http://codex.wordpress.org/Function_Reference/query_posts - 在「作者參數」標題下。

但是,挖掘源代碼揭示了其他情況:http://wordpress.taragana.net/nav.html?wp-includes/classes.php.html#query_posts

$author_array = preg_split('/[,\s]+/', $q['author']); 
for ($i = 1; $i < (count($author_array)); $i = $i + 1) { 
    $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]); 
} 
$whichauthor .= ')'; 

要查看您的逗號分隔的列表正在使用正確我會繼續前進,開始扔調試行到WP_Query::get_posts()功能。例如,$whichauthor的最終值是多少?

+0

好吧,好愚蠢的錯誤。我以爲我選擇了該頁面模板,但沒有。原始的 query_posts('author = 9,10&post_status = publish&orderby = date') 確實爲記錄工作。 – matchbranding 2010-05-14 11:50:08

0

此稍有不同的環路將工作複式次頁面或發佈使用不同的查詢參數:

<?php $my_query = new WP_Query('author=9&post_status=publish&orderby=date'); ?> 
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> 
<?php the_title(); ?></a> 
<?php endwhile; ?> 
2

我覺得這是做正確的方式:

function yclads_posts_where_followed_authors($where) { 
    global $wpdb; 
    $authors_ids = array(0,1,2,3);//array of authors IDs 

    if (count($authors_ids) > 0) 
    { 
     $where .= ' AND ' . $wpdb->posts . '.post_author IN(' . implode (',',$authors_ids) . ') '; 
    } 
    else 
    { 
     $where .= ' AND 1=2'; //do not return results 
    } 
    return $where; 
} 

add_filter('posts_where', 'posts_where_filter_authors'); 
相關問題