前提・実現したいこと
Wordpressで各ターム一覧を表示させるページを作成しています。
現在は、各ターム作成順(古いほうが上)に並んでおります。
各タームを選択した最新記事の日付順に並び替えを行いたいのですがうまくいきません。
<現在>
ターム:メロンパン
最新記事投稿日 2月1日
ターム:クロワッサン
ターム作成:メロンパンのあと
最新記事投稿日 2月2日
<実現したい表示>
ターム:クロワッサン
ターム作成:メロンパンのあと
最新記事投稿日 2月2日
ターム:メロンパン
最新記事投稿日 2月1日
該当のソースコード
<?php $postTypeName = 'report';//投稿タイプの名前 $taxonomy = 'cat_report';//タクソノミー名 $num = 1;//表示する投稿の数 -1で全部 $terms = get_terms($taxonomy); foreach ( $terms as $term ){ $args = array( 'posts_per_page' => $num, 'post_type' => $postTypeName, 'tax_query' => array( array( 'taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $term->slug, ) ) ); $myPost = get_posts($args); if($myPost): foreach($myPost as $post) : setup_postdata( $post ); ?> <li> <?php $days = 7; //Newを表示させたい期間の日数 $kiji_title = $post->post_title; $today = date_i18n('U'); $entry = get_the_time('U'); $kiji = date('U',($today - $entry)) / 86400 ; if( $days > $kiji ){ echo '<div class="new">NEW</div>'; } ?> <a href="<?php the_permalink(); ?>" class="opacity"> <?php if (has_post_thumbnail()) { the_post_thumbnail('newsImg'); } else { ?> <img src="<?php bloginfo('template_directory'); ?>/images/sample5.jpg" alt="" /> <?php } ?> <p class="ti"><?php the_title(); ?></p> <p><?php the_excerpt(); ?></p> <p class="day"><?php the_time('Y.m.d') ?><span>|<?php $terms = get_the_terms($post->ID, 'cat_report'); $t_slug=''; foreach($terms as $term){ $term_name = $term->name; $t_slug = $t_slug.$term_name.','; }; $t_slug = substr($t_slug, 0, -1); echo $t_slug; ?></span></p> </a> </li> <? endforeach; endif; wp_reset_postdata(); } ?>
試したこと
foreach ( $terms as $term ){ $args = array( 'posts_per_page' => $num, 'post_type' => $postTypeName, 'orderby' => 'date', 'order' => 'DESC', 'tax_query' => array( array( 'taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $term->slug, ) ) );

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/20 01:47
退会済みユーザー
2019/02/20 02:18
2019/02/20 06:51