投稿の有無で分岐するメモ

情報を追記している場合はありますが、古い情報を訂正はしていませんので、公開年月日を参照してください。プラグイン・タグ、いずれもワードプレス・PHPのバージョン等によって動作しない場合もあります。

2つのタクソノミーを持つ投稿の有無

タクソノミー1のターム1
 タクソノミー2の親ターム1
  親ターム1の子ターム1 親ターム1の子ターム2
 タクソノミー2の親ターム2
  親ターム2の子ターム1 親ターム2の子ターム2
タクソノミー1のターム2
 タクソノミー2の親ターム1
  親ターム1の子ターム1 親ターム1の子ターム2
~のような絞り込みリンクを作成する。

<?php $terms = get_terms( 'taxonomy2', ['parent' => 0] );
if( !empty( $terms ) && !is_wp_error( $terms ) ) { ?>
    <?php foreach($terms as $term) { ?>
<?php $args = array(
	'post_type'	=> '投稿タイプ',
	'taxonomy1'		=> 'taxonomy1-term',
	'taxonomy2' => $term->slug
); $swanyshop_posts = get_posts( $args ); if( ! empty( $swanyshop_posts ) ){ ?>
	<li>~
<?php $childs = get_terms( 'taxonomy2', ['child_of' => $term->term_id] );
if( !empty( $childs ) && !is_wp_error( $childs ) ) { ?>
		<ul>
    <?php foreach($childs as $child) { ?><?php $args = array(
	'post_type'	=> '投稿タイプ',
	'taxonomy1'		=> 'taxonomy1-term',
	'taxonomy2' => $child->slug
); $child_posts = get_posts( $args ); if( ! empty( $child_posts ) ){ ?>
                    <li><a href="~?taxonomy1=taxonomy1-term&taxonomy2=<?php echo $child->slug; ?>"><?php echo $child->name; ?></a></li>
    <?php } } ?></ul><?php } ?>
<?php } ?></li><?php } } ?>

タクソノミー1のタームは一部表示内容が違うので、全体をループにはしていない。