Make WordPress Core

Opened 14 years ago

Last modified 5 years ago

#15068 new enhancement

merging query objects/arrays

Reported by: youngmicroserf's profile youngmicroserf Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Query Keywords: needs-patch
Focuses: Cc:


As multiple post type installations proliferate, I assume more and more people will come across a situation (like me) in which they need to display two (or more) sets of content units, say posts, and post-type "audio" in one stream - say on the main blog page. As long as the only condition is the post type itself, that will work fine. However, if those two sets need to be selected using different conditional criteria, say a category in one case, and a custom field in the other, then the current query_posts options will not be sufficient.

While it is possible to retrieve multiple arrays via get_posts and merge them, then sort them using cusomt php, such a solution will break the paged navigation.

If such situations will - as I assume - become more likely as more people use multiple post types and additional variables to specify content, I think it would be useful to be able to combine multiple queries into a meta query that will sort the combined object according to a common sorting variable, say date, and thus preserve the paged navigation.

Change History (6)

#1 @Denis-de-Bernardy
14 years ago

So... In other word, add SQL UNION support?...

#2 follow-up: @scribu
14 years ago

Can you do a global sort on UNIONs ?

#3 in reply to: ↑ 2 @Denis-de-Bernardy
14 years ago

  • Milestone changed from Awaiting Review to Future Release

Replying to scribu:

Can you do a global sort on UNIONs ?

Yes, with a subquery.

  SELECT ...
  SELECT ...
) as stmt

It's a bit slow, since it'll need to quick sort or heap sort the result, but it'll still be faster and more reliable than pulling the X results of N queries, in order to drop dups and use php to sort the top X results out of the resulting merged set. Better let SQL do it than re-invent the union statement... As a bonus, using an SQL UNION allows to extract stats, i.e. the total number of rows.

Pushing this to future pending a patch, though, because it's an extremely messy proposition -- even with an ORM.

#4 @Denis-de-Bernardy
14 years ago

While we're at it: if this ever gets implemented, supporting SQL EXCEPT is only a tiny bit more work once SQL UNION is done.

#5 @wonderboymusic
11 years ago

  • Keywords needs-patch added

This would be a fun lil project for someone - not moving to 3.7 though

#6 @chriscct7
9 years ago

  • Version changed from 3.0.1 to 3.0
Note: See TracTickets for help on using tickets.