Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination showing same posts from page 1 on all other pages

Tags:

php

wordpress

I've recently had a lot of help creating an upcoming events list (see here Showing upcoming events (including todays event)?), as a result my pagination using WP Pagenavi is broken.

At the moment, when you click on page 2 it just shows the same posts as page one. Although the URL does actually change to page/2 page/3 etc.

I have this in my functions.php file:

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' );

My loop is then as follows:

<?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 ) ); } ?>
like image 505
Rob Avatar asked Jun 27 '13 16:06

Rob


1 Answers

Finally solved this with:

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' );

And:

<?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 ) ); } ?>
like image 67
Rob Avatar answered Oct 06 '22 08:10

Rob