im using paginate_links function to create pagination on my custom post type archives, no matter what i do im hitting 404 errors when going to view page 2 (ie clicking to go on one page in the pagination trail).

I’ve checked and researched and dont seem to be getting anywhere with it all. heres my before the loop query inside archive-MY_CUSTOM_POST_TYPE.php:

<?php

global $wp_query;

$args = array_merge( $wp_query->query, array( 'post_type' => 'sales', 'posts_per_page'  => 1, ) );

query_posts( $args );


if (have_posts()) :
while (have_posts()) : the_post();  

and lower down after the loops endif and above wp_reset_query i have

<?php endif; ?>

<div class="clear"></div><!-- / clear -->

<div class="pagination">

<?php 
global $wp_query, $wp_rewrite;
$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'
);

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' ) );

echo paginate_links( $pagination );
?>

</div><!-- / paginate -->  

On my loop-index.php this works with no problem, but on a loop inside Custom Post Type its a no go, i have no clashes with duplicated slugs as i have read that this can be an issue, so im taking it that it is something to do with how im querying the posts before the loop, any pointers?

regards
Martin

3 s
3

Martin’s fix works, but a better solution is to use the pre_get_posts function.

Example:

function custom_type_archive_display($query) {
    if (is_post_type_archive('custom_type')) {
        $query->set('posts_per_page',1);
        return;
    }
}

add_action('pre_get_posts', 'custom_type_archive_display');

Leave a Reply

Your email address will not be published. Required fields are marked *