Pagination fails with multiple post types in category
-
(I’m using WordPress 3.0.1).
I’m trying to display all the posts (post type ‘post’) and articles (custom post type ‘article’) in a category archive. I manage to do that, but it doesn’t work well with pagination. It seems the pages work according to the number of post-type ‘post’ posts only, although it displays also ‘article’ posts. For instance, in the “Books” category there are 4 ‘post’ posts and 2 ‘article’ posts. If I set the max posts per page to 6 or higher it shows all 6 posts, but if I set the max posts per page to, say, 1, so there should be 6 pages, then page/5/ leads to a 404 error. Note that it does display both articles and posts, but the number of pages that work is determined by the number of ‘post’ posts alone.
The same thing without a specific category works perfectly well on my main page.
This is the code I use on my main page:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'post_type' => array('post', 'article'), 'paged'=>$paged, ); query_posts($args);
On my main page that code displays all the posts in all the categories, and the pagination works perfectly.
But when I use the same code in category.php (the entire page has identical code to the page displaying my main page) it displays all the posts in all the categories, but the pagination behaves as before, i.e. if the “books” category has 4 ‘post’ posts and 2 ‘article’ posts, then it will show only 4 pages, and page/5/ will lead to 404, in spite of the fact that that code shows all the posts from all the categories and not just the “books” category.
To get the posts only in the “Books” category, I use the following code:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $cat_to_get = get_query_var('category_name'); // this gets the category name from its link $args = array( 'post_type' => array('post', 'article'), 'paged'=>$paged, 'category_name' => $cat_to_get, ); query_posts($args);
This does get all the posts only in the “Books” category, but again if this category has 4 ‘post’ posts and 2 ‘article’ posts, page/5/ leads to 404 error. Even if I set the post type in the query to ‘article’ the number of pages that work will correspond to the number of ‘post’ posts. The number of pages that work is always number-of-‘post’-posts/max-post-per-page. Any page after that will lead to 404.
The code does work well if I run it from a different file – pagination fails only in category.php, and in index.php if I try to use it to display the category archives.
I don’t know WordPress inner works, but my guess is that when you click on a category link WordPress automatically queries for posts and then calls category.php (or index.php) to display them. So my code in fact runs a second query, but the number of pages is set by the first query.
Does anyone know if that what happens and what can be done about it?
- The topic ‘Pagination fails with multiple post types in category’ is closed to new replies.