2016-11-01 13 views
0

カテゴリのように動作するカスタム分類を作成しました。イムは、この分類からの投稿を表示するには、次のループを使用して:カスタムタクソノミでループ内の子投稿を非表示にする

$exec_query = new WP_Query(array (
        'post_type' => 'cars', 
        'car_type' => 'large_cars', 
        'posts_per_page' => 100, 
        'order' => 'ASC', 
        'include_children' => false 
       )); 

        //* The Loop 
        if ($exec_query->have_posts()) { ?> 

          <?php 

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

           get_template_part('parts/loop', 'single-produkt'); 

         endwhile; ?> 

         <?php 

         //* Restore original Post Data 
         wp_reset_postdata(); 
        } 

これは、分類「大型車」に掲載されているすべての投稿を表示します。

しかし、このタクソノミーには、「SUV」、「バン」などの子カテゴリがあり、この子の投稿も表示されます。私はこれを見せたくありませんが、include_childrenは動作していないようです。

アイデア?

答えて

0

オールライトは、最初の子カテゴリのIDを取得し、その後WP_Queryアレイからそれらを除外することによって解決策を見つけた:

   $term_id = get_term_by('slug', $term, $taxonomy)->term_id; 
       $taxonomy_name = $taxonomy; 
       $termchildren = get_term_children($term_id, $taxonomy_name); 
       $exclude = ""; 

       foreach ($termchildren as $child) { 
        $term2 = get_term_by('id', $child, $taxonomy_name); 
        $exclude = $exclude . "" . $term2->term_id . ","; 
       } 
       $exclude = substr($exclude, 0, -1); 

       $args = array(
          //Rest of you args go here 
          'tax_query' => array (
           array(
            'taxonomy' => $taxonomy, // My Custom Taxonomy 
            'terms' => explode(',', $exclude), // My Taxonomy Term that I wanted to exclude 
            'field' => 'id', // Whether I am passing term Slug or term ID 
            'operator' => 'NOT IN', // Selection operator - use IN to include, NOT IN to exclude 
           ), 
          ), 
           'post_type' => $type, 
           $taxonomy => $term, 
           'posts_per_page' => 100, 
           'order' => 'ASC', 
         ); 
0

これを試してみてください。子どものタクソノミーを削除するには、以下のようにinclude_childrenを使用する必要があります。

$args = array(
      //Rest of you args go here 
      'tax_query' => array(
        array(
         'include_children' => false 
       ) 
      ), 
       'post_type' => 'cars', 
       'car_type' => 'large_cars', 
       'posts_per_page' => 100, 
       'order' => 'ASC' 
     ); 

    $query = new WP_Query($args); 
+0

は、私はこの部分を追加したときに何の投稿が示されていないallthough、ありがとう/ – BTB

+0

"large_cars"には親自体があるということと関係がありますか? – BTB

+0

タクソノミは混乱しています。おもちゃにこれを試してもらえますか?http://stackoverflow.com/questions/3449815/wordpress-how-can-i-exclude-posts-in-child-taxonomies-from-a-custom-taxonomy-qu – jannej

関連する問題