1

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.

Complete 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):
        ?><h2><?php //echo $wsc->name; ?></h2>
        <?php $childcat = $wsc->name; 
                $childid = $wsc->ID;
        ?>
        <h2><?php echo $childcat; ?></h2>
        <?php 
        $posts = get_posts(array(
      'post_type' => 'menu',
      'orderby' => 'name',
      'order' =>  'DESC',

      'tax_query' => array(
          array(
              'taxonomy' => $wsc->taxonomy,
              'term'  => $wsc->term_id,
              'include_children' => false,
          )
      ),
      'nopaging' => true,
      ));?>
                         <div class="menu-title"><?php the_title(); ?></div>
                         <div class="menu-title"><?php the_content(); ?></div>
                        <?php //endwhile; ?>
            <?php

    endforeach;
endforeach; 
?>

1 Answer 1

1

Just one note before I start, you should not call your post type menu, menu is a reserved term, so you might run into issues with default build in functionalities. You can, however, use the plural form menus without having to worry about issues ragarding conflicts

As for your code, you are using invalid parameters, which is just simply ignored by WP_Query which in turn is the reason you are getting back all posts regardless. For custom taxonomies, you should use a tax_query to query posts belonging to custom taxonomy terms

The following should work: (Note: You just need to feed the correct taxonomy name and correct term ID to the tax_query for your specific use case)

$posts = get_posts(array(
      'post_type' => 'menu',
      'orderby' => 'name',
      'order' =>  'DESC',
      'tax_query' => array(
          array(
              'taxonomy' => $wcatTerm->taxonomy,
              'terms'  => $wcatTerm->term_id,
              'include_children' => false,
          )
      ),
      'nopaging' => true,
      ));

EDIT

The category parameters will not work as you are not making use of the build in taxonomy, category. You are using a custom taxonomy, so to query posts from a specific term or terms from a custom taxonomy, you need to use a tax_query as in my sample code.

As I also stated, you need to feed the correct parameters to your tax_query as I do not know your exact setup. The terms parameter (by default) should be an integer value or an array of integer values of term ID's, so take that into account

Lets take an example from your code

$wsubcats = get_categories($wsubargs);
    foreach ($wsubcats as $wsc):

Inside this foreach loop, your tax_query values will be as follow:

  • Your taxonomy value will be $wsc->taxonomy

  • Your terms value will be (int) $wsc->term_id

EDIT 2

Sifting through your code, there are plenty errors. I have corrected all that errors in your code and have commented on them inside the code, so make sure to check my comments. Where I have stated to remove, remove that specific line of code as it is not necessary. Also, I do not know if you really need empty terms, so I have left that and just build in protection to avoid bugs and errors if there are actually empty terms

<?php
$taxonomy = 'menu-items'; // Do not use hypens in taxonomy names, only use underscores
$term_args = array(
    'hide_empty' => 0, // Why do you need empty terms? 
    'orderby' => 'ASC',  
    'parent' => 0
);
$wcatTerms = get_terms(
    $taxonomy, // Taxonomy name
    $term_args // Array of arguments to get terms by
);
// To avoid bugs and errors, run this only if we have a valid array of terms
if (    !empty( $wcatTerms ) // Check that we do not have an empty array 
     && !is_wp_error( $wcatTerms ) // Check that we do not have a WP_Error object returned
) { 
    // If our conditional check passed, then only run the following code
    foreach($wcatTerms as $wcatTerm) { // Do not use : as it is hard to debug and to read, use old style curlies
        //echo $wcatTerm->name;
        $wsubargs = array(
            //'hierarchical'=>1, // This is default, not needed and can be removed
            //'show_option_none'=>'', // Invalid parameter, where do you get this from, to remove
            'hide_empty'=> 0, // Why do you need empty terms?
            'parent'=>$wcatTerm->term_id,
        );
        //$wsubcats = get_categories($wsubargs); // Why use get_categories, get_categories uses get_terms, to remove
        $wsubcats = get_terms( 
            $taxonomy, // We have set this earlier
            $wsubargs
        );
        foreach ( $wsubcats as $wsc ) { // Again, do not use : as it is hard to debug
            ?><h2><?php //echo $wsc->name; ?></h2>
            <?php $childcat = $wsc->name; ?>
            <h2><?php echo $childcat; ?></h2>
            <?php 
               //$posts = get_posts(array( // Do not mess with $posts global, rather use $posts_array, to remove
            $args = array(
                'post_type' => 'menu', // Really bad name, reserved name in Wordpress
                'orderby' => 'name',
                'order' =>  'DESC',
                'tax_query' => array(
                    array(
                        'taxonomy' => $taxonomy, // We have defined this already right at the start
                        'terms  => $wsc->term_id,
                        'include_children' => false,
                    )
                ),
                'nopaging' => true,
            );
            $posts_array = get_posts( $args );

            // You are missing the loop here, that is why you are getting the same stuff over and over again
            if ( $posts_array ) { // Only run this if we actually have posts, if we do not have posts, we will have bugs and errors
                foreach ( $posts_array as $post ) { // You were missing this
                    setup_postdata( $post ); // VERY VERY IMPORTANT, without this, the template tags like the_content will not work
                    ?>
                             <div class="menu-title"><?php the_title(); ?></div>
                             <div class="menu-title"><?php the_content(); ?></div>

                <?php } // endforeach $posts_array
                wp_reset_postdata(); // VERY IMPORTANT to reset the $post global to main query
            } // endif $posts_array
        } // endforeach $wsubcats
    } // endforeach $wcatTerms
} // endif check for valid array of terms
?>
3
  • No problem :) I am very thankful to you you made my day and saved my hair too :P the most amazing part was you edited all this using mobile thankyou very much again :)
    – dh47
    Commented Aug 5, 2015 at 8:29
  • My pleasure, I have accepted your edit as well, thanks for that. Enjoy ;-) Commented Aug 5, 2015 at 8:30
  • Hey pieter you should help me with this please
    – dh47
    Commented Aug 7, 2015 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.