This problem is giving me a headache and I was wondering if anyone knew how to fix it.
I have a custom post type called Review. Here is how it is defined:
function codex_custom_init() {
$args = array( 'rewrite' => array('slug' => 'review'),
'public' => true,
'has_archive' => true,
'show_ui' => true,
'label' => 'Review',
'publicly_queryable' => true,
'taxonomies' => array('category', 'post_tag'),
'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail', 'author')
);
register_post_type( 'review', $args );
}
add_action( 'init', 'codex_custom_init' );
My archive page, handled by archive-review.php, works fine for pages like this
http://www.acme.com/review/page/3/
However, when categories are involved, wordpress does not display the “next” page. Instead, it goes to a 404. For example, when I try to access this:
http://www.acme.com/category/random/page/2/
Why does that give a 404? Also, it no longer is powered by archive-review.php, but archive.php (which makes sense I guess)
archive.php query stuff:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat = get_category(get_query_var('cat'));
$catID = $cat->term_id;
$args = (array('post_type' => array('post', 'review'), 'cat' => $catID, 'paged' => $paged));
query_posts($args);
Please help!! I am very frustrated!
Creating a secondary query or overwriting the main query inside a page template is the quickest and easiest way I know to break pagination.
The main query, which determines which page to load runs before your template thus the results on the page and the query that loads the page become out of sync. The main query does not know about your in-template modifications to the query. It doesn’t matter that your in-template query modifies the “main” query. The changes come too late.
What you need to be doing is use a filter on
pre_get_posts
. Unless I am reading it wrong, all you need to do is set thepost_status
.Unless you are the WordPress Core, and you aren’t,
query_posts
is never applicable. Just don’t use it.