2017-03-20 11 views
0

私は彼らの最も深いカテゴリで投稿したいと思います。例えばWP_Queryで最も深いカテゴリの投稿を取得

$posts = new WP_Query(
    array(
     'posts_per_page' => -1, 
     'post_type' => 'custom_post_type', 
     'post_status' => 'publish', 
     'tax_query' => array(
      'relation' => 'OR', 
      array(
       'taxonomy' => 'custom_category', 
       'field' => 'term_id', 
       'terms' => (Main term id) 
      ), 
      array(
       'taxonomy' => 'custom_category', 
       'field' => 'term_id', 
       'terms' => (Child term id) 
      ), 
      array(
       'taxonomy' => 'custom_category', 
       'field' => 'term_id', 
       'terms' => (Grandchild term id) 
      ) 
     ) 
    ) 
); 

(Main term id) = 1 
(Child term id) = 2 
(Grandchild term id) = 3 

私は以下の記事を取得したいと思います:

  • 投稿のみメインカテゴリ(1)を有し、任意の子を持っているかいません孫カテゴリ。
  • ポストは、メインカテゴリ(1)を持ち、子カテゴリ(2)を持ち、孫カテゴリはありません。
  • ポストはメインカテゴリ(1)、子供カテゴリ(2)、および孫カテゴリ(3)も持っています。

可能でしょうか?ご協力ありがとうございました!

答えて

0
/** you can try below code **/  
$custom_category_args = array(
     'type'      => 'custom_post_type', 
     'child_of'     => 0, 
     'parent'     => 0, 
     'orderby'     => 'id', 
     'order'     => 'ASC', 
     'hide_empty'    => 0, 
     'hierarchical'    => 1, 
     'exclude'     => '', 
     'include'     => '', 
     'number'     => '', 
     'taxonomy'     => 'custom_category', 
     'pad_counts'    => false 

    ); 
    $custom_categories = get_categories($custom_category_args); 
    foreach ($custom_categories as $custom_category) { 
     ?> 
     <a href="<?php echo get_term_link(intval($custom_category->term_id), $custom_category->taxonomy);?>"><?php echo $custom_category->name;?><a/> 
     <?php 
      $custom_type = 'custom_post_type'; 
      $custom_args=array(
       'post_type' => $custom_type, 
       'post_status'  => 'publish', 
       'posts_per_page' => -1, 
       'caller_get_posts'=> -1, 
       'hierarchical' => 1, 
       'exclude'   => '', 
       'include'   => '', 
       'number'   => '', 
       'tax_query'  => array(
             array(
              'taxonomy' => 'custom_category', 
              'field' => 'id', 
              'terms' =>$custom_category->term_id 
             ) 
            ), 
      'orderby'   => 'id', 
      'order'   => 'ASC' 
      ); 
      $custom_my_query = null; 
      $custom_my_query = new WP_Query($custom_args); 
      $custom_my_total_count = count($custom_my_query); 
      if($custom_my_query->have_posts()) 
      { 
        while ($custom_my_query->have_posts()) : $custom_my_query->the_post(); 
         ?> 
         <a href="<?php echo get_permalink();?>"><?php echo get_the_title($post->ID);?></a> 
         <?php 
         endwhile; 
      } 
      wp_reset_query($custom_my_query); // Restore global post data stomped by the_post(). 
    } 
関連する問題