Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#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:



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,

Attachments (1)

2012.zip (2.4 KB) - added by kcssm 2 years ago.
child theme for twentytwelve

Download all attachments as: .zip

Change History (16)

@kcssm2 years ago

child theme for twentytwelve

comment:1 @alexvorn22 years ago

why posts_per_page = 2?

comment:2 @alexvorn22 years ago

just remove this and I think everything will work fine

comment:3 @alexvorn22 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.

comment:4 @alexvorn22 years ago

  • Component changed from General to Query

comment:5 @kcssm2 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.

comment:6 @alexvorn22 years ago

you can't navigate because you don't use the code properly.

comment:7 @kcssm2 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.

comment:8 @alexvorn22 years ago

go to Reading Settings page and put to show 2 pages at most

comment:9 follow-up: @kcssm2 years ago

I have tried that and it does not help.

comment:10 @kcssm2 years ago

It seems like no matter how we query for galleries, it takes the post count from the default posts query in homepage.

comment:11 in reply to: ↑ 9 @alexvorn22 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.

comment:12 @kcssm2 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?

comment:13 @SergeyBiryukov2 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.

Related: #11067, #16168, #16450, #19463.

Last edited 2 years ago by SergeyBiryukov (previous) (diff)

comment:14 @mdgl2 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.

comment:15 @kcssm2 years ago

Thank you all for clarifying in such a great detail. Just love WordPress community.

Note: See TracTickets for help on using tickets.