Support » Plugins and Hacks » Hacks » Custom Post Types and Listings (pagination)

  • Hi

    I have created a custom post type (lets call it ‘events’). That means I automagically get as the listing page. The codex tells me I can the create a ‘archive-events.php’ page to customize that listing. So far so good.

    The problem is I want [x] per page (lets say 4). So, I decide to run a wp_query to get the listing (Lets assume I have 25 posts of this type):

    I then loop through it:

    <?php while ($query->have_posts()) : $query->the_post(); ?>
    [insert cool listing stuffs here]
    <?php endwhile; ?>

    Still all is good with the world. Puppies still are cute and unicorns still poop rainbows.
    But now I need pagination, so I go hunting through the codex once more, and come up with this:

    $query->query_vars['paged'] > 1 ? $current = $query->query_vars['paged'] : $current = 1;
    $pagination = array(
    	'base' => @add_query_arg('paged','%#%'),
    	'format' => '',
    	'total' => $query->max_num_pages,
    	'current' => $current,
    	'show_all' => true,
    	'type' => 'plain',
    	'prev_next' => true,
    	'prev_text' => '&laquo;',
    	'next_text' => '&raquo;',
    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' ) );

    Most of that makes enough sense to me (we are setting the base page, and it needs to know how many pages / what page we’re on etc). Hell, it even works perfectly.

    I click page 2, boom, I get the second page of content. I click page 3, magic happens, I get the third page of content. I click page 4, I get a 404 erorr. The puppies now weep, as I consider cutting the horns of the unicorns to make them nothing more than magical horses.

    Now I can only assume that because I have 25 item, WP is doing some default stuff (that assumes 10 per page), so it is 404ing as soon as I get past page 3.

    So, how do I fix that

    TL;DR: Custom post types pagination, and making WP not assume 10 per page?

  • The topic ‘Custom Post Types and Listings (pagination)’ is closed to new replies.