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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Will

    (@wlanni)

    I suppose I should state my goal: I would like to have both the pagination links AND the ability to display more items per page.

    If I need to undo pretty permalinks, that’s fine. I’d be happy with a url that looks like https://www.site.com/taxonomy-name/?term=20&page=2&numposts=25

    I’m also staying away from plugins as much as possible; I can’t afford to wait on a plugin developer to update their plugin when new site security patches are released.

    I believe that add_query_arg will not work with next/previous_posts_link. Look at the article below and try the replacement mam_add_query_arg() instead.

    https://wordpress.mcdspot.com/2010/06/02/stay-in-category/

    I have not tested this, but from reading the code for paginate_links, this might work:

    $pagination = array(
    	'base' 			=> 'https://artbeforehygiene.com/explore-the-era%_%',
    	'format' 		=> '/page/%#%/',
    	'total' 		=> $wp_query->max_num_pages,
    	'current' 		=> $current,
    	'show_all' 		=> true,
    	'type' 			=> 'plain',
    	'prev_text'		=> '<',
    	'next_text'		=> '>',
            'add_args'              => array('numposts',$numposts),
    	);

    Not quite sure about the slashes – you may need to move the first one in format back after ‘explore-the-era’.

    Thread Starter Will

    (@wlanni)

    vtxyzzy — I’m about to go play a show, so I won’t be able to get back to this until Monday.

    Thanks, though, I’ll give that article a read, and try out your suggestion.

    It would be nice to get some more documentation into some of these function pages in the Codex — they could use a little bit of expansion!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘add_query_var breaks pagination’ is closed to new replies.