Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#19985 closed defect (bug) (invalid)

Query_posts paged not working for subcategories when mixing custom post types

Reported by: novakpeter's profile novakpeter Owned by: mikew-scriptcom's profile mike@…
Milestone: Priority: normal
Severity: normal Version: 3.3.1
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

       $page = (get_query_var('paged')) ? get_query_var('paged') : 1; 

       if (is_category('news') OR is_category('events')){
        query_posts(array('post_type'=>array('post','ai1ec_event'),'paged' => $page));
       }
       
       if(have_posts()) : while(have_posts()) : the_post();


I have this code in themes category.php. It mixes two post types, standard posts and events from All in One Events Calendar (WP plugin). It works almost perfect. "News" is a normal category, in this category the loop lists normal posts, events and makes a proper pagination.

"Events" is a subcategory of "News". If I am in "Events" category, the loop lists normal posts together with events. BUT: the pagination doesnt work properly. If I click on previous_posts_link generated link, the URL is proper:

http://www.domain.com/categories/news/events/page/2/

BUT, on the page are mixed posts from news and events category together and clicking on previous_posts_link generated link leads to page 3, which "doesnt exist".

SO, THE SAME CODE WORKS PERFECTLY ON A CATEGORY, FAILS PAGING ON SUBCATEGORY

If you want, I can give you FTP details to see it in action.

Change History (7)

#1 @kawauso
13 years ago

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

The 404 error is being generated because the WordPress initially looks for page 3 of the category 'events'.

The substitution for your new query on post types only happens in the category template on existing pages, you need to hook in on a filter that alters the initial query.

There is a pre-existing ticket for a similar issue if anyone cares to find it.

#2 follow-up: @novakpeter
13 years ago

  • Cc novakpeter added
  • Resolution invalid deleted
  • Severity changed from normal to major
  • Status changed from closed to reopened

I made it simpler to show the bug (pagination is just a secondary thing):

       $page = (get_query_var('paged')) ? get_query_var('paged') : 1; 

       if (is_category('news') OR is_category('events')){
        query_posts(array('paged' => $page));
       }
       
       if(have_posts()) : while(have_posts()) : the_post();

The whole listing is bad if you are in "events" subcategory! In "news" category, it lists posts from "news" category (and pagination works) - OK. In "events" category (which is subcategory of "news"), it lists posts from "news"(!!!) category (and pagination doesnt work) - BUG. I thing this simple thing just should work (the same way for category and subcategory).

What do you mean by hooking initial query, do you mean hooking pre_get_posts? I have tried already. I was able to show normal posts from certain category and custom posts from certain category. I was not able to show normal posts from certain category and custom posts from ANY category and not assigned to any category:-( And the whole thing didnt work in parent category:-(

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

#3 in reply to: ↑ 2 @kawauso
13 years ago

it lists posts from "news"(!!!) category (and pagination doesnt work)

The query you're passing to query_posts() doesn't actually specify a category at all, so it's loading everything.

What do you mean by hooking initial query, do you mean hooking pre_get_posts? I have tried already. I was able to show normal posts from certain category and custom posts from certain category. I was not able to show normal posts from certain category and custom posts from ANY category and not assigned to any category:-( And the whole thing didnt work in parent category:-(

That's a support issue. Please take it to the forums or IRC channel.

#4 @novakpeter
13 years ago

Yes, category is not specified. And there is the point with post types back.

This (with category specified):

query_posts(array('post_type'=>array('post','ai1ec_event'),'paged' => $page,'cat' => 16));

loads post of type "post" from categery "16" and IGNORES posts of type "ai1ec_event" (posts of this type are not assigned to any categories).

In my opinion, it should show posts of type "posts" from category 16 and all posts of type "ai1ec_event" (and that is what I want) because their category is not specified, right? In this way, there is not way to show posts of two types, normal posts from specified category and all "ai1ec_event" posts...

So, the bug is now, that parameter "cat" is applied to normal posts, but destroys the "add custom post type posts" to the loop.

#5 @pavelevap
13 years ago

  • Cc pavelevap@… added
  • Keywords close added

Problem with pagination and categories can be solved by using variable $query_string: http://codex.wordpress.org/Function_Reference/query_posts#Usage_Note

I suppose that query_posts is used to COMBINE posts based on specified parameters. So, there will be posts with post types "post" and "ai1ec_event" AND related to category 16. If you know that there are no categories assigned to ai1ec_event post type, then there is no sense to query it this way?

Maybe you should make two separate queries:

1) Select some posts with category 16.

2) Select all posts with post type ai1ec_event.

And then merge selected posts, which can be based for example on published date...

#6 @helenyhou
13 years ago

  • Keywords close removed
  • Resolution set to invalid
  • Severity changed from major to normal
  • Status changed from reopened to closed

As noted, this is a support issue, not a bug, and should be addressed on the forums or IRC.

#7 @thee17
13 years ago

Related #14370

Note: See TracTickets for help on using tickets.