Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#16168 closed defect (bug) (invalid)

404 error with pagination on custom taxonomy page

Reported by: kamiyeye's profile kamiyeye Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0.4
Component: Permalinks Keywords:
Focuses: Cc:

Description

If a query with a custom 'posts_per_page' is made in a specific taxonomy template say taxonmy-portfolio_categories.php

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
query_posts(array('post_type'=>'portfolio', 'paged'=>$paged, 'posts_per_page' =>$posts_per_page, 'portfolio_categories'=>$category->name));

Assume qualified posts number is 28, $posts_per_page is 6 (thus 5 pages in total), and the option value of "Blog pages show at most" is 10, a 404 error will be returned when trying to access the 4th page or 5th page.

This only occurs with querys on custom post types or custom taxonomies, not found with querys on build-in post types or taxonomies.

Attachments (2)

taxonomy-genre.php (1.5 KB) - added by inbytesinc 13 years ago.
No_WPQuery.txt (522 bytes) - added by inbytesinc 13 years ago.

Download all attachments as: .zip

Change History (11)

#1 @kamiyeye
13 years ago

  • Severity changed from normal to major

#2 @dd32
13 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

You're doing it wrong.

Query_posts unfortunately has the effect, that it runs AFTER the query has already been made, as a result, this means that the page will 404 before your template is even loaded as expected (The query before your query_posts only has 4 pages).

You need to use a hook such as 'pre_get_posts' or you need to hook posts_request (i think that's it, not sure) and set the posts_per_page on a conditional -based on the current query-, not make a new query.

Valid places for using query_posts: None. Seriously, Use a custom WP_Query instance inline in a page if you need to have another loop inside a loop, but don't replace the main loop using query_posts.

#3 @dossy
13 years ago

  • Cc dossy@… added

#4 @inbytesinc
13 years ago

  • Component changed from General to Permalinks
  • Keywords 2nd-opinion needs-testing added
  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Version changed from 3.0.4 to 3.2.1

My 2 cents, using WP_Query as dd32 suggested:

  1. Done a fresh install of WordPress 3.2.1 (on Windows Server 2008 & IIS7) in www.mydomain.com/web. Using TwentyEleven Theme.
  1. Set up Permalinks like this: Month and Name.
  1. Created Taxonomy "Genre" in functions.php as stated here: http://codex.wordpress.org/Function_Reference/register_taxonomy. Created Custom Post Type "Books" like this: http://codex.wordpress.org/Function_Reference/register_post_type
  1. Updated Permalinks, for Flushing Rewrite.
  1. Inserted Genre: Fiction. Inserted Books: The Lord of The Rings and The Hobbit. Of course, assigned genre Fiction to both.
  1. Made a copy of the page.php file (in the TwentyEleven theme folder) and renamed as taxonomy-genre.php. Programmed the loop very similar to the example showed on this page: http://codex.wordpress.org/Function_Reference/wp_reset_postdata. NOTE: See the attached file.
  1. Browsed to: www.mydomain.com/web/genre/fiction/. Everything went fine.
  1. Clicked on "Previous Links" link: www.mydomain.com/web/genre/fiction/page/2/ --> ERROR 404.
  1. Using the code in attached file "No_WPQuery.txt" (default Loop) + [Admin Zone / Settings / Reading / Blog pages show at most --> 1], and everything will work fine.
  1. Workaround using WP_Query: http://themeforest.net/forums/thread/wordpress-custom-page-type-taxonomy-pagination/43010
  1. Other users that think it's a bug:

http://wordpress.stackexchange.com/questions/21181/custom-post-type-archive-page-set-posts-per-page-paginate
http://wordpress.stackexchange.com/questions/18853/custom-post-type-archive-404s-with-paginate-links

Please, if you agree it's a bug, set this to fix queue.

THANKS.

#5 @scribu
13 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

Using the code in attached file "No_WPQuery.txt" (default Loop) + [Admin Zone / Settings / Reading / Blog pages show at most --> 1], and everything will work fine.

Exactly, because, like dd32 said, you shouldn't be using query_posts() or new WP_Query for the main loop.

You should use the 'request' filter:

http://wordpress.stackexchange.com/questions/21341/alternative-to-query-posts-for-main-loop/21378#21378

Feel free to leave follow-up comments, but please don't reopen this ticket.

Last edited 13 years ago by scribu (previous) (diff)

#6 @scribu
13 years ago

I believe the confusion stems from the fact that query_posts() does work if you use it in a page template.

In that case, due to the multi-part page feature, WP doesn't throw a 404 but simply displays the first part of the page, when it can't find the part given in the URL.

Last edited 13 years ago by scribu (previous) (diff)

#7 @SergeyBiryukov
13 years ago

  • Version changed from 3.2.1 to 3.0.4

#8 @inbytesinc
13 years ago

  • Keywords 2nd-opinion needs-testing removed
  • Severity changed from major to normal

Firstly: Sorry for bothering reopening the ticket, scribu.

I really was in the doubt between opening a new ticket, re-opening this one, or simply adding the comment. Rejected the last one, because I was not sure if someone was going to read it, being the ticket closed. And I was almost sure (hours testing and searching a solution on Internet) that this was a bug.

Well, in my case, I've always searched for a solution working with get_posts/WP_Query to no avail.

Now it's crystal clear.

I think (from my novice point of view) that query_posts doesn't contribute too much to WordPress. If I was a WP developer I'd suggest to eliminate it one of this days... ;o)

Thanks for your suggestion link!!

#9 @scribu
13 years ago

Glad to have straightened you out, inbytesinc.

query_posts#Caveats does seem to be getting longer and longer.

Note: See TracTickets for help on using tickets.