Help Structuring Query for Archive Pages

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.

Read More

Update:
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?

Related posts

Leave a Reply

1 comment

  1. 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.