Support » Fixing WordPress » Pagination in custom query for category.php not working

  • I’ve looked at many of the previous category loop related topics on the forum but I’ve had no luck sorting this out.

    Essentially I’m running this query within category.php but I cannot get the pagination to work. If I have 6 posts, it correctly breaks down the posts into 3 per page and pagenavi shows 2 pages, however clicking on the page 2 link returns a page not found error. If I were to have three pages, pages 1 and 2 would work, but page 3 wouldn’t. It’s the last page in all cases that doesn’t work. Removing the “posts_per_page” option and setting the number of posts per page via Settings > Reading does work, but I need to specify it from the page itself. Hopefully someone can help.

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string.'&posts_per_page=3&paged='.$paged.'&cat='.$current_cat); if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php endwhile; ?>
    <?php wp_pagenavi(); ?>
    <?php endif; wp_reset_query(); ?>
Viewing 15 replies - 1 through 15 (of 18 total)
  • You shouldn’t need this bit.


    The category should already be in the query vars, so there’s no need to declare it again.

    Try an array and see if there’s any change.

    $args = array( 'posts_per_page' => 3, 'paged' => $paged );
    $args = array_merge( $args, $wp_query->query ); // Merge with the existing query vars
    query_posts( $args );

    You’re right of course, I’ve removed that redundant part.

    With your changes Mark, the exact same problem still occurs. The same query works within page.php though so I’m wondering if this is a bug with 3.0?

    Any plugins running? Not a bug, else i’d see the same problem (works fine here).

    Try deactivating plugins, and if that doesn’t help temporarily switch theme and see if you can replicate the problem.

    I’ve disabled all plugins and tried this on another theme and the problem still occurs. Do you do freelance work Mark – I’d be happy to give you my login details and pay you for your time?

    <?php if(function_exists('wp_pagenavi')) {
    		 	} else { ?>
        <div class="navigation">
          <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
          <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
      <?php }

    Then when you deactivate the wp_pagenavi plugin, you still have navigation of paged posts.

    Try redeclaring the $query_string global:

    global $query_string;

    Or create a new $wp_query altogether …

    $temp = $wp_query;  // assign orginal query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query($args);
      if( have_posts() ) :
    		while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    Freelance, nope sorry… 🙂

    In regard to your problem though i’d be about 95% confident the problem lies with code introduced by either a plugin or function in the current theme.

    Have tested the code in the Twenty Ten theme inside loop.php(since this file deals with the looping part) to confirm, and the code worked as expected.

    My test code.

    if( is_category() ) {
    	$args = array( 'posts_per_page' => 1 );
    	$args = array_merge( $args, $wp_query->query );
    	query_posts( $args );

    What version of WP are you using and which theme’s have you tested under?

    My thinking is that you have a filter running inside a plugin or the theme’s functions file that is over-riding the code you’ve posted above (my best guess at present).

    I’ve tried all that David, but unfortunately no luck.

    Mark, I’m using 3.0.1, on this theme:

    I’ve now tested my original query and your query in loop.php in the TwentyTen theme and the pagination works. This was expected since I can get it to work outside category.php on my own theme. I then tried it in category.php in the TwentyTen theme and the pagination does not work. Surely this is a bug with pagination in category.php with 3.0.1.

    I don’t fully understand the new looping system (i’ve not used it extensively – get_template_part), but i’m able to reproduce the problem described.

    Tested the following change to category.php in the TwentyTen theme.

    // The get_template_part() line is unmodified, only the query_posts line is what i've added for testing
    				query_posts( array_merge( array('posts_per_page'=>1,'paged'=>( get_query_var('paged') ? get_query_var('paged') : 1 )) , $wp_query->query ) );
    				get_template_part( 'loop', 'category' );

    I get 3 pages, then a 404 for page 4, etc… (i have 5 posts total just to clarify – test install). Then again, i’m not sure category.php is the appropriate file to set query parameters for a theme that utilises get_template_part, i’d have half assume loop parameters need be set in the loop files, ie. loop.php, or loop-category.php (if exists).

    Yes though, perhaps a bug, it really depends if query_posts should be able to declare parameters outside a loop file of a theme that uses get_template_part (i don’t know the answer to that unfortunately).

    Thanks David, but the issue is not with the pagination plugin. I removed that completely and returned back to the Older/Newer links and the problem still occurs.

    It’s definitely an issue with the query itself. This could be a bug as Mark also mentions. I’m pretty sure it was working just fine in 2.9.

    I’ll have to try and find a work around, possibly utilizing the new loop feature. If I do I’ll post the solution here.

    The workaround would be to move your conditional statements for setting the query parameters into the loop template… at least until such time functionality works correctly (i know this at least works because it’s what i tested initially)…

    One ever-so-slightly related ticket i found earlier didn’t seem to raise any questions for those responding, so i’d assume this should work and is in fact a bug. I say related in the fact that the ticket shows the same procedure, a query_posts call just prior to a call to get_template_part.

    I’ll do some more in-depth testing tomorrow and see if i can track down the cause, should make for a better bug report if i can find the source of the issue.

    I found this in another thread which could be the root of the problem.

    I did some debug and found out something interesting.
    I put at the end of the 404 template file a print_r($wp_query); and discovered that after clicking on the “next page” link on the first archive page, the posts_per_page variable is set back to the default value (that set on the admin settings page).
    In other words my setting of the posts_per_page variable written at the beginning of the archive template is completely ignored and somehow lost.
    Still I don’t know why this happens…

    It seems that the wp_query is executed before opening the archive template where there’s the “correct” custom posts_per_page value and so, considering the default value (higher than the custom one), page 2 is non existent and then 404 error is correct…

    I managed to solve the problem using a custom url rewrite rule (put on functions.php of my template) introducing the correct posts_per_page value directly on the query string at redirect and everything goes right. But this is not a solution as I hate to change 2 different files just to change the posts_per_page setting of the custom archive page. I think there should be another and hopefully better way to do this.

    This is the key. The custom query does work in category.php if you leave out the posts_per_page variable and specify it from Settings > Reading or if you set it to the same number as in Settings > Reading. But I need to specify it from the page itself. Do you know of any solution Mark.

    The problem occurs for any page not the first.

    Printing out the query array will show that the post per page setting is not maintained after the first page when setting query parameters prior to get_template_part ….

    I think the appropriate place for query parameters is inside the loop files when using the get_template_part method.

    The alternative to declaring in the loop template would be to use a filter/action to change the posts per page setting for particular categories (or all).

    One example:

    add_action( 'parse_query', 'category_posts_per_page', 10, 1 );
    function category_posts_per_page( $q_obj ) {
    	if( is_category() )
    		$q_obj->query_vars['posts_per_page'] = 1; // Example value

    Finally got around to try out these things. I could not get the get_template_part method to work, the same problems still occurred (I was probably doing it wrongly).

    However I’ve used your filter/action method and it works just fine. Probably not the cleanest way of doing things, but it’s a solid fix that works for me. Thanks Mark.

    Hi – I’m experiencing similar / same issue. (when removing the ‘posts_per_page’ parameter from query, the pagination works .. ?

    (I’m also, trying to setup pagination on ‘category’ template.)

    Anyone else have reasoning for this? I will try the filter myself, in hopes to surpass the issue .. but I would much prefer a logical reasoning to the problem we’re face with here.


Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Pagination in custom query for category.php not working’ is closed to new replies.