add_query_var breaks pagination
-
Hello,
I ran into what seems like a bug, curious if anyone else has played with this stuff, as this a very lightly documented area of WordPress.
First, off, here’s a duplicate of the page on a dev site. You’ll see the pagination working at the bottom. You’ll also see a link with the “show 20 posts per page” slapped into the mid left. Ignore the missing images, I don’t have time to transfer all that stuff over.
I’m using the pagination code from the example. This appends “/page/2/” to the end of my url when I click on a page 2 link.
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1; $pagination = array( 'base' => @add_query_arg('page','%#%'), 'format' => '', 'total' => $wp_query->max_num_pages, 'current' => $current, 'show_all' => true, 'type' => 'plain', 'prev_text' => '<', 'next_text' => '>' ); if( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' ); if( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) ); if (get_next_posts_link() || get_previous_posts_link()) echo "<div id=\"pagination\">Pages: " . paginate_links( $pagination ) . "</div>"; ?>
Then I’ve appended another variable to my url using add_query_arg to allow more posts to appear on the page than is set in the ‘posts_per_page’ arg of my WP_Query.
<?php $numposts = (intval($_GET['numposts'])) ? intval($_GET['numposts']) : 15; $args=array( 'posts_per_page' => $numposts, 'paged' => $paged, ); ?> <a href="<?php echo add_query_arg('numposts', '20', get_permalink() );?>">show 20 posts per page</a>
The problem occurs when both items are in action. Even with pretty permalinks turned off, and the pages set up to appear as &page=# the pagination links don’t work, and don’t advance to the next page or to the page number you’ve clicked, IF you’ve got the ?numposts=# active in the url.
- The topic ‘add_query_var breaks pagination’ is closed to new replies.