WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm building a site with several different post types (blog posts, products, informational articles, etc.). On archive type pages (search results, tag pages, etc.), I want to display content from the different post types in visually distinct sections. Then, the user could either select the specific page we he wants, or click a link to see all of the results from a specific post type.

How should I go about structuring my Loop? Should I use multiple WP_Query objects (one for each post type), or grab everything with one query and then separate it out with PHP? I can think of a couple ways to make this work, but I'm not sure what the best practice for performance and future proofing my code would be. Thanks.

I wound up using Alex Lane's advise, and running multiple queries. I'm circumventing the main query for these archive pages (there's no need for it) by using the technique described on the wp-hackers list, and then I'm looping through all relevant post types and running a new WP_Query for each of them. It gets the job done, but it feels pretty kludgy and inelegant to me.

Update 2:
As I think about this more, I'm wondering if it might be faster to just grab all of the posts, and then separate them into post type categories with PHP. I'm not an expert on database performance, though, so I don't know. I'm just imagining having to run a somewhat complicated search query 5-6 times (several of which will likely get no results) before the page loads. That seems really slow to me. On the other hand, pulling a massive data set from the database after just one query might be even slower. I guess the only way to know for sure would be to benchmark it, but I'm only one person, and I can't really afford the time to build and test both systems. Are there any MySQL performance experts out there that have any thoughts on this?

share|improve this question
up vote 0 down vote accepted

I would do multiple queries. I know that generally that slows stuff down, but sometimes if you're looking a page with a ton of content, it's to be expected.

share|improve this answer
That's kind of what I was leaning towards as well. It just seems like such a drag on performance. I guess there's no really good way around it though. – Dominic P May 12 '12 at 2:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.