Fun with array_merge

106 views
Skip to first unread message

Pat Ramsey

unread,
Apr 25, 2012, 1:19:29 AM4/25/12
to wordpres...@googlegroups.com
Hi all,

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:
$query1 = new WP_Query(array(
'post_type' => 'page',
'post_parent' => 341,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
));
$query2 = new WP_Query(array(
'post_type' => 'page',
'post_parent' => 109,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
));

Then I used array_merge to combine the posts array from each set of results:
$querysmash = array_merge($query1->posts, $query2->posts);

After that, it's a foreach statement to loop through the array:
foreach ($bigm as $post) : setup_postdata($post);

// Do something

endforeach; wp_reset_postdata();

Thoughts?


Pat






Paul Menard

unread,
Apr 25, 2012, 9:33:14 AM4/25/12
to wordpres...@googlegroups.com
Pat,

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

P-




--
You received this message because you are subscribed to the Google Groups "WordPress Austin" group.
To post to this group, send email to wordpres...@googlegroups.com
To unsubscribe from this group, send email to wordpress-aust...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/wordpress-austin?hl=en
 
Our meeting information is available at http://www.meetup.com/austinwordpress/

Pat Ramsey

unread,
Apr 25, 2012, 11:09:03 AM4/25/12
to wordpres...@googlegroups.com
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.

Cheers!

Pat

Bill Erickson

unread,
Apr 25, 2012, 11:33:20 AM4/25/12
to wordpres...@googlegroups.com
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.


---
Bill Erickson
WordPress Consultant
512-537-0315
http://www.billerickson.net

NOTE: To better serve my clients, partners, and most importantly my family, I've begun checking email only three times per day. 

For phone calls, please schedule here: http://tungle.me/billerickson . If it's an emergency, please call now and leave a voicemail

Pat Ramsey

unread,
Apr 25, 2012, 11:36:04 AM4/25/12
to wordpres...@googlegroups.com
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.

Cheers!

Pat

Paul Menard

unread,
Apr 25, 2012, 11:50:35 AM4/25/12
to wordpres...@googlegroups.com
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

global $wpdb;
$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. 

P-
Reply all
Reply to author
Forward
0 new messages