Custom post type archive 404’s with paginate_links

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:

Read More
<?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

Related posts

Leave a Reply

3 comments

  1. 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');
    
  2. If anyone else as the same issue my full workaround is:
    1) in wp-admin >> settings >> reading set blog posts to show as 1.
    2) then override this in loop-blog.php to posts_per_page => 10.
    3) in your custom post type loop.php files set posts_per_page => 5.

    Remember these are the settings that i require, your needs may be different, bottom line is, set blog posts in wp-admin to 1, you can configure the other number of posts_per_page to your own needs

    now all is working good, a little hacky but at least it works, so my next question is >>> is this a wp bug?