Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#23134 closed defect (bug) (duplicate)

Pagination not working in custom post type homepage when there are no posts

Reported by: kcssm's profile 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)

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

Download all attachments as: .zip

Change History (16)

@kcssm
12 years ago

child theme for twentytwelve

#1 @alexvorn2
12 years ago

why posts_per_page = 2?

#2 @alexvorn2
12 years ago

just remove this and I think everything will work fine

#3 @alexvorn2
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.

#4 @alexvorn2
12 years ago

  • Component changed from General to Query

#5 @kcssm
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.

#6 @alexvorn2
12 years ago

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

#7 @kcssm
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.

#8 @alexvorn2
12 years ago

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

#9 follow-up: @kcssm
12 years ago

I have tried that and it does not help.

#10 @kcssm
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 @alexvorn2
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 @kcssm
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 @SergeyBiryukov
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.

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

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

#14 @mdgl
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.

#15 @kcssm
12 years ago

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

Note: See TracTickets for help on using tickets.