0

I've created two divs which displays posts from two different categories. The paginate_links() function works only for the first div and not for the second.

I tried @Boone Gorges answer: Multiple WP_Query loops with Pagination

Here's my code:

<div>
<?php
      $paged1= isset($_GET['paged1'])?(int)$_GET['paged1']:1;
      $paged2= isset($_GET['paged2'])?(int)$_GET['paged2']:1;
      $newsposts=new WP_Query(array(
        'post_type'=>'post',
        'category_name'=>'news',
        'paged'=>$paged1,
        'posts_per_page'=>3
      ));


          while($newsposts->have_posts())
          {
            $newsposts->the_post();
    ?>
    //some html code
      <?php } 
        $pag_args1= array(
          'format'  => '?paged1=%#%',
          'current' => $paged1,
          'total' => $newsposts->max_num_pages,
          'add_args' => array( 'paged2' => $paged2 )
      );
      echo paginate_links( $pag_args1 );



       ?>

  </div>


//second div
<div>

    <?php

      $eventsposts= new WP_Query(array(
        'post_type'=>'post',
        'category_name'=>'events',
        'paged'=>$paged2,
        'post_per_page'=>3
      ));


          while($eventsposts->have_posts())
          {
            $eventsposts->the_post();
    ?>
//some html code
      <?php } 
        $pag_args2 = array(
          'format'  => '?paged2=%#%',
          'current' => $paged2,
          'total'   => $eventsposts->max_num_pages,
          'add_args' => array( 'paged1' => $paged1 )
      );

      echo paginate_links( $pag_args2 );


      ?>
  </div>

The links are displayed and works only for the first div; the second div does not even display its links.

The URL when clicked on the next page of the first div looks like this: http://localhost/wordpress/?paged1=2&paged2=1

I tried changing paged2=2.. this does not work either.

Thanks for your help.

  • Try wp_reset_query() after your first call to paginate_links(). wordpress.stackexchange.com/questions/144343/… – WebElaine Feb 20 '19 at 14:54
  • Tried it, Did not work. – N3mo Feb 24 '19 at 15:25
  • Even the post_per_page argument doesn't seem to work in the second div. The extra posts just overflow. – N3mo Feb 24 '19 at 15:26
0

After a week of getting stuck, found out the solution. I had to put an extra argument 'showposts' for my $eventposts and $newsposts.

| improve this answer | |

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.