[wp]カスタム投稿一覧をタクソノミー基準の並びで出力

あんまり使わないと思うけどね。

どういうことか

タクソノミーのタームがterm01、term02、term03、みたいな感じで作られていたとする。

カスタム投稿ではいくつかの投稿がされていて、ランダムにタームが付与されているとする。

記事一覧を作る際、普通だったら投稿順で出力される。

だけど、ターム毎にまとめて出力したい。

ということ。

手入力でやれないこともない

タームを指定して出力することで、タームごとのグループ分けをすることができる。

<ul>
<?php
$myQuery = new WP_Query();
$param = array(
    'posts_per_page' => '-1',
    'post_type' => 'sample',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'sample_taxonomy',
            'field' => 'slug',
            'terms' => array('term01'),
            'operator' => 'IN',
        ),
    )
);
$myQuery->query($param);
?>
<?php if($myQuery->have_posts()): while($myQuery->have_posts()) : $myQuery->the_post(); ?>
    <li><?php the_title(); ?></li>
<?php endwhile; endif; ?>
<?php
$myQuery = new WP_Query();
$param = array(
    'posts_per_page' => '-1',
    'post_type' => 'sample',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'sample_taxonomy',
            'field' => 'slug',
            'terms' => array('term02'),
            'operator' => 'IN',
        ),
    )
);
$myQuery->query($param);
?>
<?php if($myQuery->have_posts()): while($myQuery->have_posts()) : $myQuery->the_post(); ?>
    <li><?php the_title(); ?></li>
<?php endwhile; endif; ?>
⋮
</ul>

見ての通りで、めっちゃテキスト量が多いし、指定したタームだけ出力することになる。

特定のタームだけを、ってことならわかるけど、タクソノミー内の全部を出したい場合にはどうなのって話。

こんな感じ

まずプラグイン「Category Order and Taxonomy Terms Order」をいれる。
このプラグインでタームの並びを操作できるし、出力時の並びも指定できるようになる。

で、こう。

<ul>
<?php
    $args = array(
        'parent' => 0,
        'hierarchical' => 0,
        'orderby' => 'term_order',
        'order' => 'ASC'
    );
    $taxonomy_name = 'sample_taxonomy';
    $taxonomys = get_terms($taxonomy_name, $args);
if(!is_wp_error($taxonomys) && count($taxonomys)):
    foreach($taxonomys as $taxonomy):
    $tax_posts = get_posts(array(
        'post_type' => 'sample',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy_name,
                'terms' => array( $taxonomy->slug ),
                'field' => 'slug',
                'include_children' => true,
                'operator' => 'IN'
            ),
            'relation' => 'AND'
        )
    ));
?>
<?php if($tax_posts): foreach($tax_posts as $tax_post): ?>
    <li><?php echo get_the_title($tax_post->ID); ?></li>
<?php endforeach; endif; endforeach; endif; ?>
</ul>

ターム毎にリストを分けるならこう。


<?php
    $args = array(
        'parent' => 0,
        'hierarchical' => 0,
        'orderby' => 'term_order',
        'order' => 'ASC'
    );
    $taxonomy_name = 'sample_taxonomy';
    $taxonomys = get_terms($taxonomy_name, $args);
if(!is_wp_error($taxonomys) && count($taxonomys)):
    foreach($taxonomys as $taxonomy):
    $tax_posts = get_posts(array(
        'post_type' => 'sample',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy_name,
                'terms' => array( $taxonomy->slug ),
                'field' => 'slug',
                'include_children' => true,
                'operator' => 'IN'
            ),
            'relation' => 'AND'
        )
    ));
?>
<?php if($tax_posts): ?>
<h1><?php echo esc_html($taxonomy->name); ?></h1>
<ul>
<?php foreach($tax_posts as $tax_post): ?>
    <li><?php echo get_the_title($tax_post->ID); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; endforeach; endif; ?>

タームの取得を自動化して、ターム毎に該当記事群を出力する、という動き。
なのでforeachが2回使われている。

参考

WPでカスタム投稿のターム順に投稿を表示させたい
WPでカスタム投稿のターム順に投稿を表示させたいと思っています。seasonというタクソノミーのタームが春・夏・秋・冬になっており、それぞれに投稿データが複数ぶらさがっています。ソースは下

この記事が古いところに、更に参考にしてるらしい記事がリンク切れしてて、タイミング的に危なかったですね。見つけられなくなる可能性もあったかもしれない。

コメント

タイトルとURLをコピーしました