2017-06-10 5 views
0

メニュー項目と呼ばれるカスタム投稿タイプから出力しようとしていますが、何らかの理由で機能しないと、誰かが次のクエリをどのように変更してカテゴリcorreclty私はちょうど空の結果を得ています。投稿カテゴリを考慮に入れてループを変更する必要があります

<?php 
/* 
* Loop through Categories and Display Posts within 
*/ 
$post_type = 'menu_items'; 

// Get all the taxonomies for this post type 
$taxonomies = get_object_taxonomies(array('post_type' => $post_type)); 

foreach($taxonomies as $taxonomy) : 

    // Gets every "category" (term) in this taxonomy to get the respective posts 
    $terms = get_terms($taxonomy); 

    foreach($terms as $term) : ?> 



     <?php 
     $args = array(
       'post_type' => $post_type, 
       'posts_per_page' => -1, //show all posts 
       'tax_query' => array(
       array(
     'taxonomy' => 'category', 
     'field' => 'slug', 
     'terms' => array ('salads-raw-things'), 
    ), 
        array(
         'taxonomy' => $taxonomy, 
         'field' => 'slug', 
         'terms' => $term->slug, 
        ) 
       ) 

      ); 
     $posts = new WP_Query($args); 

     if($posts->have_posts()): while($posts->have_posts()) : $posts->the_post(); ?> 


        <ul class="menu-list"> 
         <li class="menu-item"><?php echo get_the_title(); ?></p> 
          <p class="number"> 
          <?php the_field('dish_number',$posts->ID); ?> 

       </li> 





     <?php endwhile; endif; ?> 


    <?php endforeach; 

endforeach; ?> 

答えて

0

tax_query引数を作成するにはループする必要があります。ループが実行されるたびにクエリが上書きされます。

あなたはこのような何か行う必要があります。そして、

foreach($taxonomies as $taxonomy) : 

// Gets every "category" (term) in this taxonomy to get the respective posts 
$terms = get_terms($taxonomy); 

foreach($terms as $term) : ?> 
    <?php 
      tax_query[] = array(
        'taxonomy' => $taxonomy, 
        'field' => 'slug', 
        'terms' =>$term       
      ); 
endforeach ; 
endforeach ; 

を、あなただけの1 WP_Query

$args = array(
      'post_type' => $post_type, 
      'posts_per_page' => -1, //show all posts 
      'tax_query' => array(
      array(
    'taxonomy' => 'category', 
    'field' => 'slug', 
    'terms' => array ('salads-raw-things'), 
), 
    $tax_query 
    ) 
    ); 

    $query = new WP_Query($args); 

ため$argsが適切にすべての分類法との間に必要な関係を設定することを忘れないでください、あなたを作成することができます。

うまくいくように、いくつかのヒントがありますようお願いいたします。

+0

私はcusotm投稿タイプ –

+0

でもそのカテゴリの下にあるすべてのアイテムを投稿できるようにしたいので、$ argsで私の答えで 'tax_query'を削除し、 'tax_query' => array($ tax_query) – Benoti

関連する問題