Q&A
WordpressのWP-Queryで記事のカテゴリーに優先順位をつけ、優先順位順で並び替えを行いたいです。
WP-Queryにはorderbyのオプションがあるようなのですが、カテゴリーベースで記事を並びかえられないようでした。
WP-Queryのページ送りを使いつつ、カテゴリーベースで並び替えをする方法を教えて頂きたいです。
カテゴリー
cate1 // こちらのカテゴリーがついている記事は、記事の最後に並べたい
cate2
cate3
その他の並び順は公開日付順で考えております。
WP-Queryの部分
<?php if (have_posts()) : ?> <ul class="p-blog__items"> <?php $paged = get_query_var('paged') ? get_query_var('paged') : 1; $args = array( 'post_type' => 'blog', 'posts_per_page' => 20, 'orderby' // カテゴリーの優先度で並びかえたい 'paged' => $paged ); $the_query = new WP_Query($args); while ($the_query->have_posts()) { $the_query->the_post(); get_template_part('template-parts/content', get_post_type()); } wp_reset_postdata(); ?> </ul> <?php else : ?> <?php get_template_part('template-parts/content', 'none'); ?> <?php endif; ?>
ページ送りの部分
<?php if ($the_query->max_num_pages > 1) : ?> <?php $limitnum = 999999999; echo '<div class="p-blog__pager">'; echo paginate_links(array( 'base' => str_replace($limitnum, '%#%', esc_url(get_pagenum_link($limitnum))), 'format' => '', 'current' => max(1, get_query_var('paged')), 'total' => $the_query->max_num_pages, 'prev_next' => false, 'type' => 'list', 'end_size' => 3, 'mid_size' => 3 )); echo '</div>'; ?> <?php endif; ?>