Here is my situation, if you look at the following page: http://staging.cancerwellness.com/mind-body/cancerversary/, you will see that the pagination is not working. After researching the problem, I found the problem within the following code:

    foreach ($categories as $cat) {
       $cat_id = $cat->term_id;

       if($cat->name != 'Uncategorized' && $cat->name != 'Hero') :
          $catURL = get_category_link($cat_id);
          echo "<div class='side-cat'><button>".$cat->name."</button><ul>";
            if (have_posts()) : while (have_posts()) : the_post(); ?>
             <li><a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
              <?php endwhile; endif; ?>
             <li><a href="<?php echo $catURL; ?>" title="Read All <?php echo $cat->name; ?> Stories">More. . .</a></li>
               <?php endif;  } ?>

The main culprit was the query_posts. Is there an alternative to the query posts so it doesn't interfere with the pagination?

I'm using Wordpress 5.1

Thank you, Kevin Davis

  • Query_posts is specifically designed to alter the main page query. For additional queries, you can create addition WP_Query objects and operate on them instead. – tmdesigned Mar 29 '19 at 16:44

query_posts() is going to alter the main query, and it doesn't sound like you want that. I would recommended using the WP_Query object to get the other posts.

That said, if you must use query_posts(), call wp_reset_query() at the end of your foreach to reset your main query back to the original one.

| improve this answer | |
  • Before I read this message, I used the wp_reset_query(); as well and I was going to answer the question. – Kevin Davis Mar 29 '19 at 21:28

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.