2015-07-10 35 views
3

我想從特定類別或特定用戶發佈帖子。看來我們在wordpress中只能有AND條件。我知道下面的代碼是錯誤的,但是這是我需要得到 - 我希望所有由特定用戶編寫的帖子或從一個特定的類別按類別或作者獲取文章wordpress

$args = array(
    'posts_per_page' => 10, 
    'offset'   => $PageStart, 
    'query' => array(
    'relation' => 'OR', /* <--    here */ 
    array(
     'author' => 18, 
    ), 
    array(
     'category' => 20, 
     ) 
    ), 
    'orderby'   => 'date', 
    'order'   => 'DESC', 
    'post_type'  => 'post', 
    'post_status'  => 'publish', 

); 
//print_r($args); 
$author_post = get_posts($args); 

請你幫助所有的職位。提前致謝 。

回答

0

我已經找到解決這個問題,我有一個自定義查詢做了合併兩個數組

SELECT DISTINCT wposts.* 
      FROM $wpdb->posts wposts 
       LEFT JOIN $wpdb->postmeta wpostmeta 
       ON wposts.ID = wpostmeta.post_id 
       LEFT JOIN $wpdb->term_relationships 
       ON (wposts.ID = $wpdb->term_relationships.object_id) 
       LEFT JOIN $wpdb->term_taxonomy 
       ON ($wpdb->term_relationships.term_taxonomy_id 
        = $wpdb->term_taxonomy.term_taxonomy_id) 
       AND wposts.post_status = 'publish' 
       AND $wpdb->term_taxonomy.taxonomy = 'category' 
       AND ($wpdb->term_taxonomy.term_id IN($cat_list) 
       OR wposts.post_author IN ($authors)) 
      ORDER BY wposts.post_date DESC 

其中$ cat_list是一個包含您的類別ID和ID作品的數組,其中包含您的作者ID。希望這會幫助有需要的人。

1

如果你想同時做get_posts() 2倍試試這個代碼

$args = array(
    'posts_per_page' => 10, 
    'offset'   => $PageStart, 
    'author'  => '18', 
    'orderby'   => 'date', 
    'order'   => 'DESC', 
    'post_type'  => 'post', 
    'post_status'  => 'publish', 

); 
$author_post = get_posts($args); 

$args = array(

    'posts_per_page' => 10, 
    'offset'   => $PageStart, 
    'category'   => '20', 
    'orderby'   => 'date', 
    'order'   => 'DESC', 
    'post_type'  => 'post', 
    'post_status'  => 'publish', 
); 
$category_post = get_posts($args); 
$total_post = array_merge($category_post,$author_post) 
+1

這將調用'AND'他不想再想要.. –

0

  1. 來自用戶的所有文章 類別
  2. 的所有帖子,然後用array_merge()
+0

我已經嘗試過,但它使分頁問題和我需要的帖子以降序顯示,所以如果我可以做到一個單一的查詢所有這些問題將得到解決。 – akhila

相關問題