Tom Osborne
Tom Osborne

Reputation: 11

WordPress: Separate posts by Post Type on Taxonomy Archive

I would like to create a custom archive page (taxonomy.php) for my Custom Taxonomies, where the posts are displayed in lists, grouped by Post Type.

I have three Post Types: -

I also have two Custom Taxonomies: -

How would I best approach this?

I have achieved something similar on a custom page template, where I have grouped a certain category of my Guides Custom Post Type by my Technical Area Custom Taxonomy terms (code below), but I can't translate this to work the way I described above.

<?php
    foreach ( $technical_area_terms as $technical_area_term ) {
        $member_group_query = new WP_Query( array(
            'post_type' => 'guides',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'technical_area',
                    'field' => 'slug',
                    'terms' => array( $technical_area_term->slug ),
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => array( 'p1000-guides', 'guides'),
                )
            )
        ) );
    ?>

    <h2><a href="../../../technical_area/<?php echo $technical_area_term->slug; ?>"><?php echo $technical_area_term->name; ?></a></h2> <!--  Technical Area Title -->
        <?php
        if ( $member_group_query->have_posts() ) : ?>

                <table>
                    <tr>
                        <th>Title</th>
                        <th>Issue</th>
                        <th>Date</th>
                    </tr>   <?php
            while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
                <tr>
                    <td><?php the_title( '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' , '</a>' ); ?></td>
                    <td></td>
                    <td><?php the_time( get_option( 'date_format' ) ); ?></td>
                </tr>

        <?php endwhile; ?> 
            </table>
        <?php   else: ?>
                No content
        <?php endif; ?>

Upvotes: 0

Views: 302

Answers (1)

ejntaylor
ejntaylor

Reputation: 2101

First let's get the posts

    // vars
    $posts = $wp_query->posts;
    $posts_sorted = array();

Then we can loop through them and sort into a new multi-dimensional array with the different post_types in the array. This will look like this:

    // loop and reorder
    foreach($posts as $key => $obj) {

        if (!array_key_exists($obj->ID, $posts_sorted))
        $posts_sorted[$obj->post_type][] = $obj;

    }

Now we just need to loop through our new array and output what we want to output. Here's my example of looping through

 <?php

    foreach($posts_sorted as $post_type => $post_arr) : ?>

        <div class="wrapper">
            <h3><?= $post_type ?></h3>

            <div class="card-group">

                <?php

                    foreach($post_arr as $post ) {
                           var_dump($post);
                    }

                ?>

            </div>

        </div>

    <?php endforeach; ?>

In this example I'm just var_dumping the post object, but you can work with it as you require.

Upvotes: 1

Related Questions