0

Situation

I am trying to paginate some records in Wordpress 4.1. I have a custom post type (events) and a category called 'training'. I have created a custom category template (category-training.php) which I use to make the query_posts().

I have the following in category-training.php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type'         =>  'events',
    'cat'               => "20", //(The training category id)
    'paged'             => $paged,
);
query_posts($args);

if ( have_posts() ) :
     while ( have_posts() ) : the_post();
        ?> the_title();<?php
     endwhile;

    $big = 999999999; // need an unlikely integer
    global $wp_query;
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
        ) 
    );
endif;

Issue

When I go to my site at www.site.com/category/training/

I see the first 12 records I expect. The pagination links are below and indicate the correct number of pages. However when I click to go to another page (or manually enter the url) I am taken to a 404 page.

Further Info

The crux of the problem seems to be when I set the post type to events. If I take that out and put a ton of regular posts in the training category, the pagination works fine. I also notice that if I then revert to specifying post_type I can sometimes get to page one or two - what I think is happening is that the pages are being set up according to the expected category posts but not including other post_types = so if there was 36 normal posts in the category - I could get to page 3 (at 12 per page).

1
  • Search here or in codex for pre_get_posts, you need to alter the main query outside of the template.
    – Milo
    Jan 15, 2015 at 15:26

1 Answer 1

0

As Milo suggested I had to alter the main query. Followed the following:

https://stackoverflow.com/questions/21954101/wordpress-3-8-1-category-page-2-error-404-not-found-custom-post-type?rq=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.