0
サイドバーにカレンダー付きのイベントページがあります。ユーザーが日付をクリックするたびに、作成したAPIエンドポイントを介して、その日付からすべてのイベントがロードされます。 これまでのところすべてが良いですが、ページネーションに問題があります。 ユーザーがイベントページに入力したばかりのページングは完璧に機能しますが、ユーザーがAPI経由でイベントを読み込むためにカレンダーに当たってページネーションが破棄され、常にnext_post_linkがページ番号2として返されます。 posts_per_pageはカスタムクエリと同じですが、それは役に立ちません。WordPress APIページネーションは2ページしか返しません。
<?php
function get_events(WP_REST_Request $request)
{
global $post;
$event_date = $request["event_date"];
$paged = ($request["paged"]) ? $request["paged"] : 1;
$date = '';
$args = array(
'post_type' => 'event',
'posts_per_page' => 3,
'paged' => $paged,
'meta_key' => 'event_date',
'meta_value' => $event_date,
'meta_compare' => '>=',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$the_query = new WP_Query($args);
ob_start();
include(locate_template('events_template.php'));
$events = ob_get_contents();
ob_get_clean();
return $events;
}
イベントテンプレート:
<div class="posts-container">
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div>
<?php
//Here we group events by event_date and add the date at the top.
$event_date = get_post_meta($post->ID, 'event_date', true);
if ($event_date != $date)
{
$event_date_formatted = date('l, F jS, Y', strtotime($event_date));
echo "<p class='page-header'><strong>$event_date_formatted</strong></p>";
$date = $event_date;
}
?>
<div class="event ath-card">
<h4><?php the_title(); ?></h4>
<p><?php echo get_post_meta($post->ID, 'event_location', true) ?></p>
<p><?php echo get_post_meta($post->ID, 'event_details', true) ?></p>
</div>
</div>
<?php endwhile; endif; ?>
</div>
<div class="loader text-center hidden">
<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
</div>
<div class="div pagination-container text-center">
<?php
//always returns page number 2.
//I run the exact query in the page itself and it works fine
next_posts_link('<button class="ath-btn ath-btn-info">LOAD MORE</button>', $the_query->max_num_pages);
?>
</div>