Select All in Parent Category, Group by Child Category?

Here’s a straight up simple solution. Requires you to have the most recent version of WordPress though. (or at least 4.1)

Using nested taxonomy query.

Taking what you have, and just adding to it a bit.

$args = array(
    'post-type'      => 'episode',
    'post-status'    => 'publish',
    'posts_per_page' => 4,
    'tax_query'      => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'series',
            'field'    => 'slug',
            'terms'    => array( 'real-deal' ),// Name of the series (in slug format)
        ),
        array(
            'taxonomy' => 'series',
            'field'    => 'slug',
            'terms'    => array( 'season-1' ),// Name of the season (in slug format)
        )
    )
);

$episodes = new WP_Query( $args );

print_r( $episodes->posts );

That’s just saying find the 4 most recent published episodes that belong to a specific series and also contain a specific season. (this is what your visual mockups appear to be portraying)

You mentioned needing the “seasons” to be dynamic.

Here’s how you can get all “subcategories” (seasons) from the parent category (series) and then retrieve the 4 latest episodes from that series, and in all possible seasons.

$taxonomy = 'series';

$seasons = get_terms( $taxonomy, array(
    'parent' => 1, // TERM ID OF THE SERIES
    'fields' => 'id=>slug'
) );

if ( ! empty( $seasons ) && ! is_wp_error( $seasons ) ) {

    $args = array(
        'post-type'      => 'episode',
        'post-status'    => 'publish',
        'posts_per_page' => 4,
        'tax_query'      => array(
            'relation' => 'AND',
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'slug',
                'terms'    => array( 'industry-news' ),// Name of the "series" (in slug format)
            ),
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'slug',
                'terms'    => array_values( $seasons ),// Name of the "seasons" (in slug format) DYNAMIC
            )
        )
    );

    $episodes = new WP_Query( $args );

    print_r( $episodes->posts );

}

Let’s say you have 3 seasons in a series… And you want to display the 4 latest episodes from each season in this series.

$taxonomy = 'series';

$seasons = get_terms( $taxonomy, array(
    'parent' => 1, // TERM ID OF THE SERIES
    'fields' => 'id=>slug'
) );

$season_episodes = array();

if ( ! empty( $seasons ) && ! is_wp_error( $seasons ) ) {

    foreach ( array_values( $seasons ) as $season ) {

        $season_episodes[ $season ] = array();// Placeholder in-case there's no episodes found.

        $args = array(
            'post-type'      => 'episode',
            'post-status'    => 'publish',
            'posts_per_page' => 4,
            'tax_query'      => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => $taxonomy,
                    'field'    => 'slug',
                    'terms'    => array( 'industry-news' ),// Name of the "series" (in slug format)
                ),
                array(
                    'taxonomy' => $taxonomy,
                    'field'    => 'slug',
                    'terms'    => array( $season ),// Name of the "seasons" (in slug format) DYNAMIC
                )
            )
        );

        $episodes = new WP_Query( $args );

        if ( ! empty( $episodes->posts ) ) {
            $season_episodes[ $season ] = $episodes->posts;// Add all episodes found in this season.
        }

    }

}

if ( ! empty( $season_episodes ) ) {
    print_r( $season_episodes );
}

This last method will take the latest 4 episodes in a series put those 4 posts into separate arrays categorized by seasons.

So all said and done, you’ll have an array for each season in the series containing 4 recent episodes. All you have to do is output the information.

Leave a Comment