2

I've been trying to sort this out for a while with no luck so maybe someone can point me in the right direction.

enter image description here

I created a CPT called reports and have two custom Taxonomies under it called report-locations and report-weeks. I want a page that only queries reports with both report-weeks and report-locations.

The reports will be listed under each report-weeks and then under that Week they should list the report-locations and I'd like to be able to order them somehow.

So far I have the following which gets me close but it relies on a secondary function to grab and list the report-locations and therefore doesn't allow me to order those posts since they aren't part of the WP_Query.

<?php
$post_type = 'reports';
$weeks = get_terms( array(
    'taxonomy' => 'report-weeks',
    'orderby' => 'name',
    'order' => 'DESC'
) );

foreach ( $weeks as $week ) :

    $cat_query = new WP_Query( array(
        'post_type' => $post_type,
        'tax_query' => array(
            array(
                'taxonomy' => $week->taxonomy,
                'field' => $week->slug,
                'terms' => $week->term_id,
            ),
        ),
        'posts_per_page' => '-1',
    ) );

    if ( $cat_query->have_posts() ) : ?>
<h2><?php echo $week->name; ?></h2>

<?php while ( $cat_query->have_posts() ) : $cat_query->the_post(); ?>
<article id="<?php echo 'report-' . $post->ID; ?>">
    <h3><?php list_custom_taxonomy('report-locations', $post->ID); ?></h3>
<div><?php the_content(); ?></div>
</article>
<?php endwhile; wp_reset_postdata(); ?>
<?php endif; endforeach; ?>
  • OK, so what exactly are you trying to achieve with this code? How should they be sorted? – Krzysiek Dróżdż Jun 8 '18 at 16:30
  • @KrzysiekDróżdż They are supposed to be sorted by Week and then list all the Locations with content underneath each Week. I want to be able to sort the Locations alphabetically though. – tylorreimer Jun 8 '18 at 16:35
1

Disclaimer: It won't be very efficient solution and I hope you don't have many terms in these 2 taxonomies (if so, then you should do it a little bit different and include some custom SQL queries).

OK, disclaimer done, we can go to the solution ;) And here it goes:

<?php
    $post_type = 'reports';
    $weeks = get_terms( array(
        'taxonomy' => 'report-weeks',
        'orderby' => 'name',
        'order' => 'DESC'
    ) );
    $locations = get_terms( array(
        'taxonomy' => 'report-locations',
        'orderby' => 'name',
        'order' => 'ASC'
    ) );
    $results = array();

    foreach ( $weeks as $week ) {
        $week_results = array();
        foreach ( $locations as $location ) {
            $posts = new WP_Query( array(
                'post_type' => $post_type,
                'tax_query' => array(
                    array(
                        'taxonomy' => $week->taxonomy,
                        'terms' => $week->term_id,
                    ),
                    array(
                        'taxonomy' => $location->taxonomy,
                        'terms' => $location->term_id,
                    ),
                ),
                'posts_per_page' => '-1',
            ) );

            if ( $posts->have_posts() ) {
                $week_results[ $location->term_id ] = $posts;
            }
        }
        if ( ! empty( $week_results ) ) {
            $results[$week->term_id] = $week_results;
        }
    }

    foreach ( $weeks as $week ) :
        if ( ! array_key_exists( $week->term_id, $results ) ) continue;
    ?>
        <h2><?php echo $week->name ?></h2>
        <?php
            foreach ( $locations as $location ) :
                if ( ! array_key_exists( $location->term_id, $results[$week->term_id] ) ) continue;

                $query = $results[$week->term_id][$location->term];
        ?>
            <h3><?php echo $location->name; ?></h3>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <h4><?php the_title(); ?></h4>
                <div><?php the_content(); ?></div>
            <?php endwhile; ?>
        <?php endforeach; ?>
<?php
    endforeach;
    wp_reset_postdata();
?>

So what do we do in there? First we iterate through every week and every location and prepare WP_Query for every pair . This way we can omit the pairs that don't have any posts assigned to them.

After preparing these queries we can output the results. So we have to iterate one more time and print the posts assigned to given pair .

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.