skip to Main Content

In my Query, I’m having trouble displaying the data the way I want.
I have a custom post type with categories (custom taxonomy) that have child categories. For example:

Adults

  • sports
  • music

Teens

  • games
  • music

I have a page for each parent category (Adults, Children, etc.) in which I want to list the posts grouped by child category. For example, in the Adults page, I will have all the posts of the Adults category. These posts will be grouped by child category (sports, music, etc). For example:

Adults page (only displays posts in the Adult category)

Sports

  • post 1
  • post 2

Music

  • post 3
  • post 4

Teens page (only displays posts in the Teens category)

Games

  • post 5
  • post 6

Music

  • post 7
  • post 8

I managed to get my query to work, but it prints out all the posts regardless of categories:

Adults (page)

Adults (category – should not be displayed)

  • post 1
  • post 2
  • post 3
  • post 4

Sports

  • post 1
  • post 2

Music

  • post 3
  • post 4

Teens (from here to the end – should not be displayed in Adults page)

  • post 5
  • post 6
  • post 7
  • post 8

Games

  • post 5
  • post 6

Music

  • post 7
  • post 8

It seems the taxonomy terms are not added correctly. Here is my query so far:

        <?php
    /**
    *
    * Loop through some custom post types and group them by taxonomy term,
    * outputting the taxonomy term names before the set of posts
    *
    */

    // get all of the custom taxonomy terms
    $taxonomy = 'groupe';
    $args = array(
        'orderby' => 'id',
        'order' => 'ASC',
        'term' => 'adultes',
    );
    $taxonomy_terms = get_terms($taxonomy, $args);

    // if there are some taxonomy terms, loop through each one and get the posts in that term
    if($taxonomy_terms) {
        foreach($taxonomy_terms as $taxonomy_term) {

            $args = array(
                'post_type' => 'activite',
                "$taxonomy" => $taxonomy_term->slug,
                'post_status' => 'publish',
                'posts_per_page' => -1,
            );

            $query = new WP_Query( $args );

            if ( $query->have_posts() ) : ?>

          <!-- output the taxonomy name for the current term -->
                <h2><?php echo $taxonomy_term->name; ?></h2>

                <div class="cpts-wrap">

          <!-- loop over the posts -->
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <h3><?php the_title(); ?></h3>
                <?php endwhile; ?>

                </div><!-- .cpts-wrap -->

            <?php wp_reset_postdata(); // so nothin' weird happens to other loops
            endif;

        }
    }
    ?>

Thanks for any help!

2

Answers


  1. Chosen as BEST ANSWER

    Sorry for late response. Turns out i was completeley wrong. Since I'm listing custom post types and not pages. All I have to do is create a taxonomy template (and not a page template). That takes care of the first taxonomy level. All that is left is group activities by sub categories.

    Here's my code

            <?php
    
        $this_term = get_queried_object();
        $args = array(
            'parent' => $this_term->term_id,
            'orderby' => 'slug',
            'hide_empty' => true
        );
        $child_terms = get_terms( $this_term->taxonomy, $args );
    
        if(!empty($child_terms)) {
            foreach ($child_terms as $term) {
    
                // List the child topic
                echo '<div class="bloc-activite">';
                echo '<h2>' . $term->name . '</h2>';
                echo '<div class="section-activite">';
                // Get posts from that child topic
                $query = new WP_Query( array(
                    'post_type' => 'activite',
                    'orderby' => 'title',
                    'order' => 'ASC',
    
                    'tax_query' => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => $this_term->taxonomy,
                            'field'    => 'slug',
                            'terms'    => array( $term->slug ),
                        )
                    )
                ) );
    
                // List the posts
                if($query->have_posts()) {
                    while($query->have_posts()) : $query->the_post(); ?>
    
                // insert template part or code below
                    <?php get_template_part(  'partials/content', "activite" );  ?>
    
                    <?php
                endwhile;
            }
            echo "</div></div>";
    
    
        } //end foreach
    }
    else  {
        echo '<p class="no-content">Message if no activities.</p>';
    }
    ?>
    

  2. Given, this is the archive page for the given taxonomy the below code should work

    <?php
        /**
        *
        * Loop through some custom post types and group them by taxonomy term,
        * outputting the taxonomy term names before the set of posts
        *
        */
    
        // Get the current queried Taxonomy Object
        $current = get_queried_object();
            
        // get all of the custom taxonomy terms
        $taxonomy = 'groupe';
        $args = array(
            'orderby' => 'id',
            'order' => 'ASC',
            'term' => 'adultes',
            'parent'=>$current->term_id,
        );
        $taxonomy_terms = get_terms($taxonomy, $args);
    
        // if there are some taxonomy terms, loop through each one and get the posts in that term
        if($taxonomy_terms) {
            foreach($taxonomy_terms as $taxonomy_term) {
    
                $args = array(
                    'post_type' => 'activite',
                    "$taxonomy" => $taxonomy_term->slug,
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                );
    
                $query = new WP_Query( $args );
    
                if ( $query->have_posts() ) : ?>
    
              <!-- output the taxonomy name for the current term -->
                    <h2><?php echo $taxonomy_term->name; ?></h2>
    
                    <div class="cpts-wrap">
    
              <!-- loop over the posts -->
                    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                        <h3><?php the_title(); ?></h3>
                    <?php endwhile; ?>
    
                    </div><!-- .cpts-wrap -->
    
                <?php wp_reset_postdata(); // so nothin' weird happens to other loops
                endif;
    
            }
        }
        ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search