2013-11-09 75 views
0

我已經創建了一個自定義類型的帖子,並且我想要獲取所有在類別和標籤中的帖子。不幸的是,我不知道爲什麼它返回只有一個職位(我應該至少有3或4)WordPress無法獲取尊重標籤和類別條件的所有帖子

這是我的代碼:

$tag = $_GET["tag"]; 
$cat = $_GET["cat"]; 

$args = array(
    'post_type' => 'post_product', 
    'post_status' => 'publish', 
    'numberposts' => -1, 
    array(
     array(
      'taxonomy' => 'cating' 
     ), 
     array(
      'taxonomy' => 'tagging') 
    ) 
); 

$custom_query = new WP_Query($args); 

if ($custom_query->have_posts()): 

    while ($custom_query->have_posts()) : 
     $custom_query->the_post(); 

     $product_terms = wp_get_object_terms(get_the_ID(), 'cating'); 
     $product_terms_tag = wp_get_object_terms(get_the_ID(), 'tagging'); 

     if(!empty($product_terms) && !empty($product_terms_tag)){ 
      if(!is_wp_error($product_terms) && !is_wp_error($product_terms_tag)){ 

       foreach($product_terms as $term){ 
        if(strcmp($term->name, $cat) == 0) { 

         foreach($product_terms_tag as $term_tag){ 
          if(strcmp($term_tag->name, $tag) == 0) { 

           // display here 

          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

有沒有人有一個想法,爲什麼我因爲他們是尊重病情的職位,所以只能獲得一個職位或沒有職位。

謝謝。

回答

1

您的Taxonomy Query parameters形式不正確。而且您也沒有使用任何$_GET變量。

假設你通過$_GET越來越術語ID,它應該是這樣的:

$args = array(
    'post_type' => 'post_product', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'tax_query' => array(
     'relation' => 'AND', 
     array(
      'taxonomy' => 'tagging', 
      'field' => 'id', 
      'terms' => (int) $tag 
     ), 
     array(
      'taxonomy' => 'cating', 
      'field' => 'id', 
      'terms' => (int) $cat 
     ) 
    ) 
); 
+0

謝謝你你回答。我實際上得到了術語名稱。我在測試'strcmp($ term-> name,$ cat)== 0'中使用了我的get變量,所以我的想法是通過循環檢索所有帖子並根據條件顯示它們。我已經試過你的代碼並刪除了條件,結果並不尊重'$ cat'的值。 – Copernic

+0

你也可以在'field'參數中使用'slug'而不是'id'。 – RRikesh

+0

您還應該使用'posts_per_page'而不是'numberposts'。 – RRikesh

0

加$ POST_COUNT到$ args數組你想要的職位數

相關問題