免责声明:这不是一个非常有效的解决方案,我希望您在这两个分类法中没有太多术语(如果有,那么您应该做得稍微不同,并包括一些自定义SQL查询)。
好的,免责声明完成,我们可以转到解决方案;)下面是:
<?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();
?>
那么我们在那里做什么呢?首先,我们每周和每个地点进行迭代,并准备
WP_Query
每双鞋都有。这样,我们就可以省略没有任何帖子分配给它们的对。
准备好这些查询后,我们可以输出结果。因此,我们必须再次迭代并打印分配给给定对的帖子。