WordPress.org

Ready to get started?Download WordPress

Forums

Pagination in custom query for category.php not working (19 posts)

  1. ghostpool
    Member
    Posted 3 years ago #

    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.

    <?php
    
    $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(); ?>
    
    CODE HERE
    
    <?php endwhile; ?>
    <?php wp_pagenavi(); ?>
    <?php endif; wp_reset_query(); ?>
  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    You shouldn't need this bit.

    &cat='.$current_cat

    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 );
  3. ghostpool
    Member
    Posted 3 years ago #

    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?

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    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.

  5. ghostpool
    Member
    Posted 3 years ago #

    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?

  6. David Sader
    Member
    Posted 3 years ago #

    <?php if(function_exists('wp_pagenavi')) {
    			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>
        </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;
    query_posts($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(); ?>
  7. Mark / t31os
    Moderator
    Posted 3 years ago #

    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).

  8. ghostpool
    Member
    Posted 3 years ago #

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

    Mark, I'm using 3.0.1, on this theme: http://ghostpool.com/wordpress/mammoth

    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.

  9. Mark / t31os
    Moderator
    Posted 3 years ago #

    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).

  10. David Sader
    Member
    Posted 3 years ago #

  11. ghostpool
    Member
    Posted 3 years ago #

    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.

  12. Mark / t31os
    Moderator
    Posted 3 years ago #

    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.

  13. ghostpool
    Member
    Posted 3 years ago #

    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.

  14. Mark / t31os
    Moderator
    Posted 3 years ago #

    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
    }
  15. ghostpool
    Member
    Posted 3 years ago #

    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.

  16. mjoanisse
    Member
    Posted 3 years ago #

    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.

    Cheers!

  17. Gene
    Member
    Posted 3 years ago #

    Just a thought.

    Could it be that you are altering the query in between two pagination nav calls.

    If so the nav above the loop would be getting the paged values for the original query and the one below the loop would be getting paged values for your custom query. If this is the case creating your custom query above the top nav call should solve this.

    -Gene

  18. mjoanisse
    Member
    Posted 3 years ago #

    Thank you for reply emhr.
    -

    I don;t think so, here is the what i have.

    <?php
    		if (is_category(15)) {
    		$queried_category = 15;
    		} else if (is_category(13)) {
    		$queried_category = 13;
    		} else if (is_category(18)) {
    		$queried_category = 18;
    		}
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // allow for pagination
    
    		$args = array (
    		'cat' => $queried_category,
    		//'posts_per_page' => 3,
    		'paged' => $paged
    		);
    
    		query_posts($args);
    		?>
    
            <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
            // content here
            <?php endwhile; ?>
    
            <div class="pagination">
            <?php previous_posts_link('< previous page', 0); ?>
            <?php next_posts_link('next page >', 0); ?>
            </div><!--End pagination-->
    
            <?php else : endif; ?>
  19. homemrobo
    Member
    Posted 3 years ago #

    Hi guys, just to say thanks. I got a similar script with a custom pagination and custom order in the category.php, maybe this can help others:

    [resolved] its possible to swicth the wp_query on category.php?

    @Mark/t31os, why I cannot put "order" and "orderby" to work with query_posts? I don't get it.

Topic Closed

This topic has been closed to new replies.

About this Topic