How to add pagination to wp_query

I have a custom loop and I’m trying to break it up into several pages but I’m not getting the next and previous links to show. Here is my code:

                <?php

                    if ( get_query_var( 'paged' ) )
                        // On a paged page.
                        $paged = get_query_var( 'paged' );

                    else if ( get_query_var( 'page' ) )
                        // On a "static" page.
                        $paged = get_query_var( 'page' );

                    else
                        // Default.
                        $paged = 1;

                    $args = array(
                        'post_type'      => 'property',
                        'orderby'        => 'meta_value',
                        'meta_key'       => 'random_775',
                        'order'          => 'ASC',
                        'paged'          => $paged,
                        'posts_per_page' => 10,
                    );
                ?>

                <?php $count = 0;

                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post(); ?>

                        <?php next_posts_link('Next' ); ?>
                        <?php previous_posts_link('Back' ); ?>

I’m not sure what I’m doing wrong or is someone can shed some light on it.

Read More

Thanks in advance!

Related posts

2 comments

  1. Page link functions depend on the value in the $paged global variable. So, we need to update that variable as well as the parameter in the custom query.

    If we are on a “static” front page, the 'paged' query variable should be undefined and the 'page' query variable may be defined.

    If neither query variable is defined we are probably on page 1.

    // Only needed if this code is called via a function.
    global $paged;
    
    if ( get_query_var( 'paged' ) )
        // On a paged page.
        $paged = get_query_var( 'paged' );
    
    else if ( get_query_var( 'page' ) )
        // On a "static" page.
        $paged = get_query_var( 'page' );
    
    else
        // Default.
        $paged = 1;
    
    $args = array(
        'post_type'      => 'property',
        'orderby'        => 'meta_value',
        'meta_key'       => 'random_775',
        'order'          => 'ASC',
        'paged'          => $paged,
        'posts_per_page' => 10,
    );
    

    Code adapted from the Pagination WordPress Codex page.

    EDIT:

    $count = 0;
    
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) :
        $loop->the_post();
    
        next_posts_link( 'Next', $loop->max_pages );
        previous_posts_link( 'Back' );
    
  2. For simplicity, I strongly recommend using the WP PageNavi plugin. It’s beautiful, and super easy to implement. Below the loop, you just add wp_pagenavi(); and you’re done.

    The code you have in right now is for next/previous post, which would be used on a single post page. What you’re looking for is posts_nav_link(), also explained here.

Comments are closed.