12

There are a few other questions about this (and WP_Query pagination seems to be a huge question for a lot of people) so I'm trying to narrow down exactly how to make it function.

I'm able to create a single custom loop with pagination this code:

// http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$args = array(
    'showposts' => 2,
    'paged' => $paged
);
$wp_query->query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();

// The Post
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';

endwhile;
// http://codex.wordpress.org/Function_Reference/paginate_links#Examples
$big = 999999999;
$pag_args = array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
);
echo paginate_links($pag_args);
$wp_query = null;
$wp_query = $temp;

...but naturally if I copy/paste this loop, it works as an exact clone, meaning, when you click on "Page 2" it takes you to Page 2 for BOTH the loops.

Is there a way to break these apart from each other so each paginates separately?

Here's the full code complete with duplicate loop if anyone is interested in setting up their own local version and toying with it: http://paste.pocoo.org/show/573108/

1

1 Answer 1

20

Yes, it can be done. The key is to make the format parameter different for the two queries:

    <!-- Cats -->
    <div class="animals">
        <?
            $paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
            $paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;

            // Custom Loop with Pagination 1
            // http://codex.wordpress.org/Class_Reference/WP_Query#Usage
            $args1 = array(
                'paged'          => $paged1,
                'posts_per_page' => 2,
            );
            $query1 = new WP_Query( $args1 );

            while ( $query1->have_posts() ) : $query1->the_post();
                the_title();
                echo '<br>';
                the_category(' ');
                the_excerpt();
                echo '<hr>';
            endwhile;

            // http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
            $pag_args1 = array(
                'format'  => '?paged1=%#%',
                'current' => $paged1,
                'total'   => $query1->max_num_pages,
                'add_args' => array( 'paged2' => $paged2 )
            );
            echo paginate_links( $pag_args1 );
        ?>
    </div>

    <!-- Dogs -->
    <div class="animals">
        <?
            // Custom Loop with Pagination 2
            $args2 = array(
                'paged'          => $paged2,
                'posts_per_page' => 2,
            );
            $query2 = new WP_Query( $args2 );

            while ( $query2->have_posts() ) : $query2->the_post();
                the_title();
                echo '<br>';
                the_category(' ');
                the_excerpt();
                echo '<hr>';
            endwhile;

            $pag_args2 = array(
                'format'  => '?paged2=%#%',
                'current' => $paged2,
                'total'   => $query2->max_num_pages,
                'add_args' => array( 'paged1' => $paged1 )
            );
            echo paginate_links( $pag_args2 );
        ?>
    </div>
12
  • Thank you so much for replying Boone. I see what you're saying, but whenever I try to change the get_query_var, it throws an error. Here's the updated code: paste.pocoo.org/show/573208 The error: Call to a member function get() on a non-object in C:\wamp\www\wordpress_wp_query_pagination\wp-includes\query.php on line 27
    – Cory
    Commented Mar 29, 2012 at 16:21
  • Two things: 1) You are doing some odd things with the $wp_query global. (2) There are some implementation oddities in WP_Query itself that make it impossible to use get_query_var() and some other functions with it, so I generally reference the $_GET superglobal directly, making sure to sanitize by casting to int. I've updated my answer with a full working example. Commented Mar 29, 2012 at 17:55
  • Awesome Boone! Thanks so much, works great. Time to study it thoroughly. The weird WP_Query stuff was from this 2008 article: weblogtoolscollection.com/archives/2008/04/19/… I figured maybe the get_query_var was fetching the same var, but didn't have the brain to get it directly with $_GET. Again, thanks a lot man. Been fighting with this forever.
    – Cory
    Commented Mar 29, 2012 at 21:10
  • Cool. Good luck! Commented Mar 29, 2012 at 21:17
  • Ah crap. Care to keep fighting this with me? :) After testing your version I noticed it doesn't go back to page 1. It gets stuck on page 2. Also, when you click on a different page, it resets the other loop. Is it possible to chain these $_GET variables together so it remembers foo.com/?paged_1=2&paged_2=3 ?
    – Cory
    Commented Mar 29, 2012 at 21:31

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