0

I've recently had a lot of help creating an upcoming events list (see here Showing upcoming events including todays event - StackOverflow), 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 ) ); } ?>

Doing print_r ($query); before removing the filter produced this:

enter image description here

20
  • 1
    Do you do any other queries earlier? Could you var_dump get_query_var('paged') just before creating this query and show us what's inside it? Commented Jun 28, 2013 at 8:36
  • @KrzysiekDróżdż It says int(0) on the first page, int(2) on page 2, the url is also changing but it's just showing the content from the first page on all other pages. This worked fine when it was just a standard query on a page but since having to move it to the functions.php and add and date range it's not working correctly.
    – Rob
    Commented Jun 28, 2013 at 8:43
  • 1
    Could you print_r $query variable just after creating it (before removing filter), please? Commented Jun 28, 2013 at 8:58
  • @KrzysiekDróżdż Ok updated question with a screenshot (open it in a new tab to see full view).
    – Rob
    Commented Jun 28, 2013 at 9:03
  • 2
    @Rob: You can copy the whole var_dumped code and paste 'em into Pastebin within a <pre> </pre> tag and can highlight the syntax in PHP. I tested and it worked. Thanks. Commented Jun 28, 2013 at 13:27

2 Answers 2

0

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 ) ); } ?>
0

this is code to set paged

   if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }

and code for querry

$my_query = new WP_Query( array( 'posts_per_page'=>6,'category_name'=>'san-pham', 'paged' => $paged ) );

this work fine. You have problems like me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.