|Fun with array_merge||Pat Ramsey||4/24/12 10:19 PM|
I ran into a situation today where I had to merge two queries into one & I haven't had to do that till now. I found some documentation on merging a second query into the primary one (everything used query_posts, not WP_Query), but nothing on taking two new queries & smashing them together. I think I got it right (I got nice output) but I wanted to put this out there for others to see. I'd love feedback on it. I don't know what kind of performance hit this might cause or if there's a better way to do it.
( I've got the code on github: https://gist.github.com/2486644 )
What I have is a page template that needs to have a listing of two groups of child pages.
I wrote a query for both:
Then I used array_merge to combine the posts array from each set of results:
|Re: [wordpress-austin] Fun with array_merge||Paul Menard||4/25/12 6:33 AM|
Unfortunately, the 'post_parent' field only accepts a single int value. Not an array like many other items. Bummer. So doing multiple queries is the fact and no way around it.
IF the number of children pages for each is small (less than 10-12) AND the number of hierarchy levels is low (1-2) then I would stick with the multiple queries. and implement WP_Super Cache or something. But if your site is large with possibly multiple dozen child pages and deep hierarchy levels then you might want to consider some form of caching of the children page IDs from each WP_Query object. Then once the cache is set you can simply pass the array of IDs to a single WP_Query for the 'post__in' parameter. Then you would not need the 'post_parent'.
|Re: [wordpress-austin] Fun with array_merge||Pat Ramsey||4/25/12 8:09 AM|
That's what I ran into, Paul.
I needed two sets of child pages for this instance. Every other time I've been able to craft the query args so I got what I wanted. This time… not so lucky.
I saw an example of grabbing the IDs & passing those into a query. I'd still have to run a couple of queries to get those, though.
Caching will be done on the final launch, so hopefully performance won't be too bad. It's not a large query, regardless.
|Re: [wordpress-austin] Fun with array_merge||Bill Erickson||4/25/12 8:33 AM|
The only other approach I would try would be an actual SQL query: http://codex.wordpress.org/Class_Reference/wpdb
I'm horrible at SQL queries though so couldn't be of help there.
|Re: [wordpress-austin] Fun with array_merge||Pat Ramsey||4/25/12 8:36 AM|
That came up on Stackexchange a couple of times. I think I'd have to like self-induced pain & suffering more for me to try & write a SQL query.
|Re: [wordpress-austin] Fun with array_merge||Paul Menard||4/25/12 8:50 AM|
Well to start you can take one of the WP_Query objects and get the last SQL run as a starting point.
echo "request=[". $query1->request ."]<br />";
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_parent = 341 AND wp_posts.post_type = 'page' AND (wp_posts.post_status = 'publish') ORDER BY menu_order ASC ;
You would want to modify the SQL to include the second page_parent like
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_parent IN (341, 109) AND wp_posts.post_type = 'page' AND (wp_posts.post_status = 'publish') ORDER BY menu_order ASC ;
Assuming the above query is assigned to the variable $query_str you would simply run this via wpdb->get_results(); like the following
$post_pages = $wpdb->get_results($query_str);
Though this will not work if you have more than one level of children. Internally WordPress does multiple queries to grab children, grand-children, great grand-children etc.