#23134 closed defect (bug) (duplicate)
Pagination not working in custom post type homepage when there are no posts
Reported by: | kcssm | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.5 |
Component: | Query | Keywords: | |
Focuses: | Cc: |
Description
Hello,
This is quite confusing to put into words. So I have made a child theme for twentytwelve. Please activate this theme and make sure you don't have any post. Now create 3 new galleries i.e. custom post type that I have defined in this child theme. Now visit the homepage and try to navigate to next page, it gives an error. Even though there are 3 galleries, we cannot go to next page.
I hope you understand what I mean. Don't know if this is a bug.
Thank you,
Sanam
Attachments (1)
Change History (16)
#3
@
12 years ago
Pagination is calculated before the template is loaded. This is basically the same as using query_posts(), but since you are also altering posts_per_page, then using 'paged' isn't enough, because *total* pages was calculated against the original, main one.
#5
@
12 years ago
Yes the pagination is calculated with reference to the number of posts and because of this i cannot navigate to the next page as there are no posts.
#7
@
12 years ago
Sorry for the poor coding. But can you please show me how can I navigate to next gallery in the above mentioned condition.
#10
@
12 years ago
It seems like no matter how we query for galleries, it takes the post count from the default posts query in homepage.
#11
in reply to:
↑ 9
@
12 years ago
Replying to kcssm:
I have tried that and it does not help.
I am not sure what makes the pagination not to work but I'm 100% sure this is not a bug.
#12
@
12 years ago
So you got the issue I am talking about. Yes it must work but it does not. If not bug, what is causing this issue?
#13
@
12 years ago
- Milestone Awaiting Review deleted
- Resolution set to duplicate
- Status changed from new to closed
Duplicate of #22299.
pre_get_posts
hook should be used instead.
#14
@
12 years ago
Yes, to clarify, you can't do this sort of thing in the template (index.php)!
The problem is that the determination of whether a 404 "page not found" should be returned is done before your code is ever executed.
Instead, the current page index is checked against the main query (for "posts") and if out of range, a 404 is generated. In your case, you have no posts, so this will be true for everything except the first page.
When your template is loaded, you perform a new query and this will correctly setup links to the various pages for your custom post type "gallery". The problem is that those links, once selected are checked for a 404 against the main query as described above and before your code has had a chance to execute.
Does that make sense? Modifying the main query using the pre_get_posts
hook can be tricky but is really the only correct way of achieving what you want.
child theme for twentytwelve