0

tldr: On my production site only, when I set category_name to any category my pagination does not show up. Once I remove category_name from my wp_query the pagination shows up (for all posts on my site, of course).

The code & full problem

I have a page and set it to my page template with the following code:

<div>
  <?php
    $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
    $args = array(
      'post_type' => 'post',
      'post_status' => 'publish',  
      'posts_per_page' => 1,
      'paged' => $paged,
      'category_name' => 'videos',
    );
    $my_query = new WP_Query($args);


    if ($my_query->have_posts()):

      while ($my_query->have_posts()):
        $my_query->the_post(); ?>
  
        <article>
          <?php the_title(); ?>
        </article>
      <?php
      endwhile;
    endif; ?>

    <div id="my-pagination">
      <?php 
        echo paginate_links( array(
          'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
          'total'        => $my_query->max_num_pages,
          'current'      => max( 1, get_query_var( 'paged' ) ),
          'format'       => '?paged=%#%',
          'show_all'     => false,
          'type'         => 'plain',            
          'prev_next'    => true,
          'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
          'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
          'add_args'     => false,
          'add_fragment' => '',
        ));
      ?>
    </div>
</div>

My 1 post shows up as expected (FWIW I have 33 posts in the Videos category), but the pagination does not. If I increase posts_per_page to 30 then I see 30 of my Videos posts but no pagination.

I tested this out in my local development environment and it works as expected.

Debugging steps

Some steps I've taken to debug:

  • Exported my production database and imported it locally into a fresh install as well as imported all my files from production to local and I still do not see this issue locally
  • Downgraded WordPress from 6.1.1 to 6.0.3
  • I tried using wp_reset_query and wp_reset_postdata
  • Tried disabling plugins 1 by 1
  • Tested using the Query Loop Gutenberg block. This exhibits the same issue: as soon as I add a category filter the pagination disappears

Ask

How else can I debug this production only issue?

Update 1

Output of var_dump($my_query->request);

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (31) ) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 1

debug output 0

debug output 1

debug output 2

Update 2

debug screen v2

Update 3.5

update

16
  • 1
    Clear your caches, keep the 'category_name' => 'videos' in your args, then add var_dump( $my_query->max_num_pages, $my_query->request ); right below your new WP_Query call, and share what the dump says. Now add 'suppress_filters' => true to your args, reload the page, and see if the same issue persists - still no pagination showing?
    – Sally CJ
    Commented Mar 4, 2023 at 3:23
  • 1
    Looking at the SQL, did you change the posts_per_page to 5? (in the question, it's 1) Anyway, are you certain the Videos category contains 30 or more than 5 published posts? If yes, then yes, that $my_query->max_num_pages value is wrong. Try var_dump( $my_query ); and add the entire output to your post/question.
    – Sally CJ
    Commented Mar 4, 2023 at 19:53
  • 1
    Actually, I just wanted to know/see the value of $my_query->found_posts, but apparently, WPSE cut the full dump output (and maybe you should just remove that from your post?), so try this: install this plugin and activate it, then visit the "WPSE 414370" admin page, and follow the instructions at the bottom of the page.
    – Sally CJ
    Commented Mar 5, 2023 at 10:37
  • 1
    Now it's getting more and more like a caching issue? Or something is filtering the found_posts value?? 🤔 Anyway, I've updated my plugin, so try that revised version and let's see what the other test cases (3 & 4) say. (PS: I've deleted my gist and created a repo instead)
    – Sally CJ
    Commented Mar 6, 2023 at 3:43
  • 1
    Tests 4 and 5 were good, so I suggest you to do a completely clean install of WordPress (no plugins and using a default theme like 2023, and with caching disabled) on a subdomain or domain on the same server (or under the same hosting account), and install & activate my plugin there, then create some (dummy) posts and publish them under a category named Videos. Visit my plugin admin page and then share a screenshot of the page.
    – Sally CJ
    Commented Mar 7, 2023 at 3:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.