1

I have several problems to get the pagination work.

I have a custom page template, in there i need to display all blog posts. I build up the following "query" and build up my html structure:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 
    'post_type'         => 'post',
    'orderby'           => 'date',
    'order'             => 'DESC',
    'post_status'       => 'publish',
    'posts_per_page'    => 5,
    'paged'             => $paged,
);
$posts = get_posts( $args );

if ( ! empty( $posts ) && ! is_wp_error( $posts )) {
   $displaylist = '<div class="list-group">';

    foreach ( $posts as $post ) {
        /* loop stuff */
    }

    $displaylist .= '</div>';

    echo( $displaylist );
    wp_pagenavi( array( 'type' => 'multipart' ) );
}

with this i get the posts as i expected but not the pagination controls.

Please note: it's not the "main" page, it's a sub page.

After research i notice many use this:

$posts = new WP_Query( $args );

instead of this:

$posts = get_posts( $args );

if i use the WP_Query($args) i get many empty posts while i have only 3 posts.

I have no idea what i do wrong but I'm a absolutely newbie to wordpress and php.

3

Couple of issues here;

  • Don't use get_posts for paginated queries. It legally breaks/ignore pagination as it passes no_found_rows=true to WP_Query. Further more, get_posts only returns the $posts property from the query object, and pagination needs other properties from the query object to calculate pagination. You can paginate get_posts, but it is a really messy affair, and get_postsis just a wrapper function forWP_Query`.

  • Do not use the global $posts variable (or any other global variable in Wordpress or PHP) as a custom variable. This breaks the global variables as you are assigning different content to the variable. This is the most likely issue that is causing the inconsistent results from your query with WP_Query. Always use custom variables. The only exception to this is when you are using setup_postdata( $post ) with get_posts or similar functions, setup_postdata() requires the use of the $post global. Here you need to remember to use wp_reset_postdata() after you are done to restore the $post global

  • When using get_posts, you don't need the check for a WP_Error object as get_posts only return an array of posts or an empty array. It will never return a WP_Error object

You should try the following:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 
    'post_type'         => 'post',
    'orderby'           => 'date',
    'order'             => 'DESC',
    'post_status'       => 'publish',
    'posts_per_page'    => 5,
    'paged'             => $paged,
);
$q = new WP_Query( $args ),

if ( $q->have_posts() ) {

    $displaylist = '<div class="list-group">';

    while ( $q->have_posts() ) {
    $q->the_post();

        // LOOP STUFF HERE

    } // endwhile

    $displaylist .= '</div>';

    wp_pagenavi( array( 'query' => $q ) );

    wp_reset_postdata(); // VERY VERY IMPORTANT

} // endif
  • thanks for your reply. Now the new WP_Query($args) produce no errors but the pagination is still not working. – Manu Zi Sep 20 '15 at 13:25
  • O crap, sorry, I forgot the pagination part, will update the code ;-) – Pieter Goosen Sep 20 '15 at 13:26
  • My pleasure. Enjoy ;-) – Pieter Goosen Sep 20 '15 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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