Make WordPress Core

Opened 13 years ago

Closed 12 years ago

#17339 closed defect (bug) (duplicate)

Bug w/ Custom Post Type Archive Page and WP_Query

Reported by: whatadewitt's profile whatadewitt Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Query Keywords: has-patch
Focuses: Cc:

Description

Hi,

I have a custom post type on my blog, for it I have a custom archive page.

My reading settings in the admin were set to display 10 posts per page (default), where in my theme, I had a WP_Query with the "posts_per_page" variable set to 4.

If my custom post type had 154 posts, I could get to page 16, but going to the "next" page was giving me the 404. If I had 216, I could get to page 22, but not to page 23. (154 / 10 would give me 16 pages, 216 / 10 would give me 22 pages).

I went into my reading settings and changed the default values of 10 to -1, and it appears to have fixed the error, but this seems more like a bug than something I require support with, and I haven't found anything else like this in here, so I thought this was the best place for it.

Thank you,

--d

Attachments (1)

17339.diff (900 bytes) - added by scribu 13 years ago.

Download all attachments as: .zip

Change History (8)

#1 @scribu
13 years ago

  • Keywords reporter-feedback added

Please share the WP_Query code you have in your theme.

#2 @whatadewitt
13 years ago

I didn't write this code, so please forgive it's "all over the place"-edness

I have the following

$items = get_option("featured_blog_cats");
$args = array(
                'include'=>implode(",", $items),
                'taxonomy'=>'fbblogcat',
                'hide_empty'=>0
              );

I don't modify the args array, but there is some work done with it, there's then the following after a bit of work is done:

 $args = array( 'posts_per_page'=>4 );	
 $args = array_merge( $args, $wp_query->query );
 query_posts($args);

 // loop starts here

I have before added

echo "<pre>";
print_r($args);
echo "</pre>";

Which has given me:

Array
(
    [posts_per_page] => 4
    [post_type] => [CUSTOM POST TYPE NAME]
)

on my page.

If it helps, I did some checking. It wasn't as if it was grabbing 10 results, then only displaying the first four and on page 2 I had posts 11 - 14, it was showing the correct posts... but it appeared as though that 10 in my settings was causing issues.

Appreciate your time looking into this matter!

--d

#3 @scribu
13 years ago

If my custom post type had 154 posts, I could get to page 16, but going to the "next" page was giving me the 404. If I had 216, I could get to page 22, but not to page 23. (154 / 10 would give me 16 pages, 216 / 10 would give me 22 pages).

So, if you reached the last page and then afterwards got a 404, what is the problem? That there's a "Next" link that leads to a 404?

What happens then after you change the setting to -1?

Also, from the code you posted, I don't see where the post_type parameter is first set.

#4 @whatadewitt
13 years ago

No no.

I'm displaying only 4 posts at a time of my custom post type, but the "reading" settings say 10.

I had 154 posts, so I should have 39 pages, however, when I get to page 16 and hit "next" to get to page 17, I'm getting a 404.

It's almost as if the 10 in the reading settings is what is being used to determine if I should have a 16th page and it's completely ignoring that I'm only displaying 4 at a time.

I switched that 10 to -1 and it worked fine, but it appears as though the archive page logic is deciding that I can't have a 17th page because there aren't enough posts, ignoring the fact that I'm only displaying 4 per page.

This maybe could be changed a "minor" bug, as the case I'm getting this is quite specific to this one site, but I feel as though it needs to be reported so that maybe a change could be made so others don't get this problem in the future.

Thank you,

--d

@scribu
13 years ago

#5 @scribu
13 years ago

  • Keywords has-patch added

17339.diff is a tentative fix.

I couldn't find the $paged global defined anywhere and we should be reducing the number of used globals anyway.

Let me know if it works for you.

#7 @SergeyBiryukov
12 years ago

  • Component changed from General to Query
  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

pre_get_posts hook should be used instead.

See #11067, #16168, #16450, #19463. Closing as a duplicate of #16450.

Note: See TracTickets for help on using tickets.