0

I am querying the taxonomy terms and then listing the posts that are under those terms. The taxonomy is basically a category for a custom post type, and the terms in it are the categories.

I didn't think this was possible, but I have a foreach loop that is supposed to loop through each term(category) from the query and then list the terms(categories) on the page and then list the posts in those terms(categories) underneath them, and the issue is that it is listing the posts from all categories(terms) under every category. So I am getting multiples of each post. The posts are only added under one category.

I couldn't figure out why it was duplicating the posts until I accidently tried getting a variables value for the category description before it was even declared, when I seen that it did not get the descriptions value for the first category but it did the second one, I knew it was getting multiple categories posts at once for the single foreach loop. Is this even possible? Can someone tell me what my issue is and help me rectify the problem?

<?php   

    $myterms = get_terms('menu-category', 'orderby=none&hide_empty');  
        foreach ($myterms as $term) : ?>
            <h3><?php echo $term->name; ?></h3> <?php
                $term_name = $term->slug;
                $term_desc = $term->description;
            if( $term_desc )
              { ?>
              <div class="menu-intro"><p><?php echo $term_desc; ?></p></div>
      <?php } else { ?>
              <div class="menu-intro"></div>
      <?php } 

                $args = array(
                'post_type' => 'restaurant-menu',
                'taxonomy' => $term_name,
                );

           //  assigning variables to the loop
           global $wp_query;
           $wp_query = new WP_Query($args);

           // starting loop posting only
           while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

                    <ul class="dishes">
                                            <li>
                            <h4 class="menu-item"><?php the_title(); ?></h4>
                            <p class="menu-description"><?php the_content(); ?></p>

                        <?php $price_value = get_field( "show_price" ); 
                        if( $price_value == "Yes")
                        { ?>
                         <i class="price">$ <?php $price_value ?></i>
                  <?php } ?>
                        </li>

         <?php  endwhile;

    endforeach; 
     ?>
0

1 Answer 1

1

I found my issue was that I was using the term as the taxonomy, which brought me all posts under the taxonomy and not the category(term).

This was a quick fix, I just wish I would have caught on to it last night:

$args = array(
    'post_type' => 'restaurant-menu',
    'tax_query' => array(
        array(
            'taxonomy' => 'menu-category',
            'field' => 'slug',
            'terms' => $term_name
        )
    )
);
0

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.