我正在尝试筛选自定义查询的分页结果。我有4篇帖子,每页3篇,总共2页。当我在第1页应用过滤器时,它会起作用。当我在第2页应用过滤器时,查询结果为空。
我的猜测是,要么查询不知道我在哪个页面上,要么我的表单action=“错误”。有什么提示吗?
<div class="row justify-content-md-center">
<div class="col-9 m-4 pt-4 pb-4">
<form action="" method="get">
<label>Select option</label>
<select name="filter_options">
<option value="">Any</option>
<option value="800">800</option>
<option value="1200">1200</option>
</select>
<button type="submit" class="btn btn-primary">Filter</button>
</form>
</div>
</div>
<div class="row justify-content-md-center">
<?php
$currentPage = get_query_var( \'paged\', 1 );
if (array_key_exists(\'filter_options\', $_GET)) {
$value = $_GET[\'filter_options\'];
$args = array(
\'post_type\'=> \'my_custom_type\',
\'paged\' => $currentPage,
\'posts_per_page\'=>3,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'meta_key\' => \'my_meta_key\',
\'value\' => $value)
)
);
} else {
$args = array(
\'post_type\'=> \'my_custom_type\',
\'paged\' => $currentPage,
\'posts_per_page\'=>3
);
}
$posts = new WP_Query($args);
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post();
get_template_part( \'/template-parts/content\', \'filters\');
}
}
wp_reset_query();
?>
</div>
<div class="row justify-content-md-center mt-5">
<div class="col-2">
<?php echo paginate_links(array(
\'total\' => $posts->max_num_pages,
\'prev_text\' => \'Prev\',
\'next_text\' => \'Next\'
)); ?>
</div>
</div>