Skip to main content
3 of 4
edited title
dh47
  • 50
  • 2
  • 16

Posts from all the categories are being displayed instead of particular category

Created a custom post type Menu it has Categories and Sub-Categories.

I am succeeded in fetching Sub-Categories of the post using this code.

<?php
    $wcatTerms = get_terms('menu-items', array('hide_empty' => 0, 'orderby' => 'ASC',  'parent' =>0));
    foreach($wcatTerms as $wcatTerm) : 
        //echo $wcatTerm->name;
        $wsubargs = array('hierarchical'=>1,'show_option_none'=>'','hide_empty'=>0,'parent'=>$wcatTerm->term_id,'taxonomy'=>'menu-items');
        $wsubcats = get_categories($wsubargs);
        foreach ($wsubcats as $wsc):
           $childcat = $wsc->name; ?>
            <h2><?php echo $childcat; ?></h2>
     
        endforeach;
    endforeach; 

Later as per my requirement I want to displayed post based in sub-category for that I used this query

<?php 
        $posts = get_posts(array(
              'post_type' => 'menu',
              'orderby' => 'name',
              'order' =>  'DESC',
              'taxonomy' => $wcatTerm->taxonomy,
              'term'  => $wcatTerm->slug,
              'category' => '$childcat',
              'nopaging' => true,
              ));
              foreach($posts as $post) {
                  //echo $wsc->name;
              setup_postdata($post); ////set up post data for use in the loop (enables the_title(), etc without specifying a post ID--as referenced in the stackoverflow link above)
            ?>

But What I am getting was Categories being listed as they should but ALL the posts are being repeated for every sub-category.

As you can see in query I already added 'category' => '$childcat', but still i am not getting filter applied,Please someone help me on getting this.

dh47
  • 50
  • 2
  • 16