我最近在创建即将到来的活动列表方面得到了很多帮助(请参见此处Showing upcoming events including todays event - StackOverflow), 因此,我的分页使用wp_pagenavi()
已损坏。
目前,当你点击第2页时,它只显示与第一页相同的帖子。虽然URL实际上更改为第/2页/3页等。
我有这个functions.php
文件:
function filter_where( $where = \'\' ) {
$where .= " AND post_date >= \'" . date("Y-m-d") . "\'";
return $where;
}
add_filter( \'posts_where\', \'filter_where\' );
$query = new WP_Query(
array(
\'post__not_in\' => array(4269),
\'paged\' => get_query_var(\'paged\'),
\'post_type\' => \'whatson\',
\'exclude\' => \'4269\',
\'post_status\' => \'future,publish\',
\'posts_per_page\' => 20,
\'order\' => \'ASC\'
)
);
remove_filter( \'posts_where\', \'filter_where\' );
我的循环如下:
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// content
<?php endwhile; // end of the loop. ?>
<?php if (function_exists(\'wp_pagenavi\')) { wp_pagenavi( array( \'query\' => $query ) ); } ?>
正在执行
print_r ($query);
在拆除过滤器之前,会产生以下情况:
最合适的回答,由SO网友:Rob 整理而成
最终通过以下方式解决了此问题:
function my_filter_where( $where = \'\' ) {
global $wp_query;
if (is_array($wp_query->query_vars[\'post_status\'])) {
if (in_array(\'future\',$wp_query->query_vars[\'post_status\'])) {
// posts today into the future
$where .= " AND post_date > \'" . date(\'Y-m-d\', strtotime(\'now\')) . "\'";
}
}
return $where;
}
add_filter( \'posts_where\', \'my_filter_where\' );
以及:
<?php
$wp_query = array(
\'post__not_in\' => array(4269),
\'paged\' => get_query_var(\'paged\'),
\'post_type\' => \'whatson\',
\'exclude\' => \'4269\',
\'posts_per_page\' => 20,
\'order\' => \'ASC\',
\'orderby\' => \'date\',
\'post_status\' =>array(\'future\',\'published\'));
query_posts($wp_query);
?>
<?php
if ($wp_query->have_posts()) {
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
Content
<?php endwhile; // end of the loop.
} ?>
<?php if (function_exists(\'wp_pagenavi\')) { wp_pagenavi( array( \'query\' => $wp_query ) ); } ?>