2つのクエリを結合した際のページネーション
-
アーカイブページで先頭にSeamless Sticky Custom Post Typesを使用して固定表示し、その下にその他の投稿を表示させるべく、2つのクエリを作成し結合しております。
しかし、その場合正常にページネーションが動作しません。ページネーションで使用しているプラグインはWP-Page-Naviになります。
また1ページに最大12件の投稿を行いたいです。(管理画面上での設定はすでにしてあります)こちらのリンクを参考に制作したのですが、最後がarray型で返ってくることからどのように記述していいのかわからないためお分かりになる方ご教授いただけますと幸いです。
https://www.fourier.jp/blog/display-custom-posts-fixed-at-the-top<div class ="gallery-wrapper">
<?php
$term_object = get_queried_object(); // タームオブジェクトを取得
$term_slug = $term_object->slug; // タームスラッグ
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$sticky_args = [
'posts_per_page' => -1,
'order' => 'DESC',
'post_status' => 'publish',
'post_type' => 'gallery',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'gallery-cat',
'field' => 'slug',
'terms' => $term_slug,
),
),
'post__in' => get_option('sticky_posts')
];
$args = [
'posts_per_page' => -1,
'order' => 'DESC',
'post_status' => 'publish',
'post_type' => 'gallery',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'gallery-cat',
'field' => 'slug',
'terms' => $term_slug,
),
),
'post__not_in' => get_option('sticky_posts')
];
$sticky_query = new WP_Query($sticky_args); // 固定表示の記事を取得
$the_query = new WP_Query($args); // 固定表示以外の記事を取得
$new_query = new WP_Query();
$new_query->posts = array_merge( $sticky_query->posts, $the_query->posts );
$display_posts = array_slice($new_query, (max(1, get_query_var('paged')) - 1) * 12, 12); // 1ページに10記事ごと取得する
?>
<?php
if($new_query -> have_posts()):
while ( $new_query -> have_posts() ) : $new_query -> the_post();
?>
//ここにループ投稿
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class ="page-nation">
<?php
if(function_exists('wp_pagenavi')):
wp_pagenavi(array('query'=>$new_query));
endif;
?>
</div>
3件の返信を表示中 - 1 - 3件目 (全3件中)
3件の返信を表示中 - 1 - 3件目 (全3件中)
トピック「2つのクエリを結合した際のページネーション」には新たに返信することはできません。