Looping Through Categories of a CPT

I am trying to create tabs using the Foundations framework, outputting posts from a custom post type called testimonial and filtering them through their assigned custom category called filed-under. What am I doing wrong in my loop?

What I Have Working

  • Category tabs appear when new categories are created

What is Not Working

  • The posts aren't appearing in the tabbed content area
  • How do you get the loop to filter the 'all' category?

Link to Demo http://staging-newmummycompanyca.temp312.kinsta.cloud/testimonials/

Code

        ?php
        // TABBED HEADERS
        echo 'ul class=tabs data-tabs id=example-tabs';
        echo 'li class=tabs-title is-active link-no-style';
        echo 'a href=#all aria-selected=trueAll/a';
        echo '/li';
        $args = array(
          'hide_empty' = 1,
          'orderby'   = 'name',
          'order'     = 'ASC',
          'post_type' = 'testimonial',
          'taxonomy'  = 'filed-under',
        );
        $categories = get_categories($args);
        foreach ($categories as $category) {
          echo
            'li class=tabs-title link-no-style
                a href=#' . $category-slug . ' aria-selected=true role=tab data-toggle=tab    
                  ' . $category-name . '
                /a
              /li';
        }
        echo '/ul';

        // TABBED CONTENT
        echo 'div class=tabs-content data-tabs-content=example-tabs';
        echo 'div class=tabs-panel is-active id=' . $category-slug . '';
        echo 'ul class=accordion data-accordion data-allow-all-closed=true';
        foreach ($categories as $category) {
          $the_query = new WP_Query(array(
            'post_type'     = 'testimonial',
            'post_status'   = 'publish',
            'category_name' = $category-slug,
          ));
          while ($the_query-have_posts()) : $the_query-the_post();
            echo 'li class=accordion-item data-accordion-item';
            echo 'a href=# class=accordion-titlep';
            the_title();
            echo '/p/a';
            echo 'div class=accordion-content data-tab-content';
            echo the_field('testimonial');
            echo '/div';
            echo '/li';
          endwhile;
          wp_reset_postdata();
        }
        echo '/ul';
        echo '/div';
        echo '/div';
        ?

CODE V2

?php 
        echo 'ul class=tabs data-tabs id=example-tabs';
        echo 'li class=tabs-title is-active link-no-style';
        echo 'a href=# aria-selected=trueAll/a';
        echo '/li';  

        $_terms = get_terms(array('filed-under'));
        foreach ($_terms as $term) {

        // TABBED HEADERS 
        echo 'li class=tabs-title link-no-style';
        echo 'a href=#' . $term-slug . 'aria-selected=true role=tab data-toggle=tab';
        echo $term-name;
        echo '/a';
        echo '/li';
        }  // CLOSE OFF FIRST LOOP 
        echo '/ul';

        foreach ($_terms as $term) :
        $term_slug = $term-slug;
        
        // QUERY
        $_posts = new WP_Query( array(
          'post_type'         = 'testimonial',
          'posts_per_page'    = -1, 
          'tax_query' = array(
            array(
              'taxonomy' = 'filed-under',
              'field'    = 'slug',
              'terms'    = $term_slug,
            ),
          ),
        )); 
        
        // TABBED CONTENT
        echo 'div class=tabs-content data-tabs-content=example-tabs';
        echo 'div class=tabs-panel id=' . $term_slug . '';
        echo 'ul class=accordion data-accordion data-allow-all-closed=true';
        if( $_posts-have_posts() ) :  while ( $_posts-have_posts() ) : $_posts-the_post(); 
        echo 'li class=accordion-item data-accordion-item';
        echo 'a href=# class=accordion-title';
        echo 'p';
        the_title(); 
        echo '/p';
        echo '/a';
        echo 'div class=accordion-content data-tab-content';
        echo the_field('testimonial');
        echo '/div';
        echo '/li'; 
        endwhile; endif; wp_reset_postdata();
        echo '/ul';
        echo '/div';
        echo '/div';
        endforeach; 
        ?

Topic tabs loop custom-post-types Wordpress

Category Web


' publish',

This is your problem, an extra space character.

Note that in future you can catch this by using Query Monitor and by checking if posts were found. Because the post loop is missing an if ( $query->have_posts() ) { ... } else { echo "none found"; } type check, you had no way of knowing where to look for the problem.

Also, you're listing terms in the filed-under taxonomy:

'taxonomy'  => 'filed-under',

But then you're passing those terms to the category_name parameter, which is unrelated. So now the terms are being used as if they are terms in the category taxonomy, but they are not.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.