Ever need to know if a certain post was contained in an array returned by get_posts() or get_pages()?. Me too! Usually, I would manually loop over the array and do a bit of discovery. I recently came up with a new trick to re-key post arrays that is a bit cleaner.
If you run something like:
$pages = get_pages(); print '<pre>' . print_r( $pages, true ) . '</pre>';
… something similar to the following will be printed to the screen (example truncated for brevity):
[0] => stdClass Object [ID] => 675 [post_author] => 5 [post_date] => 2010-07-25 19:40:01 [post_date_gmt] => 2010-07-25 19:40:01 [post_content] => This site is using the standard ... [post_title] => About The Tests [1] => stdClass Object [ID] => 501 [post_author] => 6 [post_date] => 2010-08-01 09:42:26 [post_date_gmt] => 2010-08-01 16:42:26 [post_content] => The last item in this page's content ... [post_title] => Clearing Floats [2] => stdClass Object [ID] => 174 [post_author] => 5 [post_date] => 2007-12-11 16:25:40 [post_date_gmt] => 2007-12-11 06:25:40 [post_content] => Level 1 of the reverse hierarchy test ... [post_title] => Level 1 ) [3] => stdClass Object [ID] => 173 [post_author] => 5 [post_date] => 2007-12-11 16:23:33 [post_date_gmt] => 2007-12-11 06:23:33 [post_content] => Level 2 of the reverse hierarchy test. [post_title] => Level 2
Both get_posts() and get_pages() will return an indexed array of post objects. The keys in these arrays reflect the order in which the posts were returned from the database. I have always preferred the way that get_children() returns it results. It too returns an indexed array of post objects, however the array is indexed by post id. This is very helpful in many contexts.
I’ve recently discovered the not-so-new WordPress core function wp_list_pluck(). With the help of this function, I was able to come up with the following few lines to re-key the arrays returned by get_posts() and get_pages().
$pages = get_pages();
if ( ! empty( $pages ) ) {
$ids = wp_list_pluck( $pages, 'ID' );
$pages = array_combine( $ids, $pages );
}
Now you get something a bit easier to work with:
[675] => stdClass Object [ID] => 675 [post_author] => 5 [post_date] => 2010-07-25 19:40:01 [post_date_gmt] => 2010-07-25 19:40:01 [post_content] => This site is using the standard ... [post_title] => About The Tests [501] => stdClass Object [ID] => 501 [post_author] => 6 [post_date] => 2010-08-01 09:42:26 [post_date_gmt] => 2010-08-01 16:42:26 [post_content] => The last item in this page's content ... [post_title] => Clearing Floats [174] => stdClass Object [ID] => 174 [post_author] => 5 [post_date] => 2007-12-11 16:25:40 [post_date_gmt] => 2007-12-11 06:25:40 [post_content] => Level 1 of the reverse hierarchy test ... [post_title] => Level 1 ) [173] => stdClass Object [ID] => 173 [post_author] => 5 [post_date] => 2007-12-11 16:23:33 [post_date_gmt] => 2007-12-11 06:23:33 [post_content] => Level 2 of the reverse hierarchy test. [post_title] => Level 2



Taking this technique one step further you can pluck another field for the values and combine into nice flat array of something specific like titles.
That’s a great idea! There are definitely cases where all I need is the ID and title or ID + slug.
Looks like Rarst is getting us all on the wp_list_pluck bandwagon : )
I still haven’t put it to good use. I need more samples!!! Get one it!
This is a pretty nice use of wp_list_pluck().
However, instead of writing this:
I just have a little helper function:
Thanks Scribu! I agree, a helper function makes all the sense in the world here. BTW, I was looking through the source of your Posts 2 Posts plugin on Github today when I came across tests.php. Awesome! I’m going to use this file as a guideline to write my own tests.
Please don’t use it as a guideline. :)
It says “automated testing suite” but it’s actually just some code I threw together to set up some connection types.
You would be better off looking at unit tests:
https://github.com/nb/wordpress-tests
Thanks for the link! After Aaron Jorbin’s talk at WordCamp San Francisco on unit testing with javascript I’ve really been wanting to see examples of similar code for php. The WordPress tests will be perfect to dig into. I usually write ad-hoc tests that get lost/deleted. I really like the idea of keeping them around forever!
Cool. Note that the official WP unit tests are here:
http://unit-tests.trac.wordpress.org/browser
wp_list_pluck is neat, i recently discovered it as well, i was getting blogroll bookmarks, and i wanted to check for one that already existed, since you can add the same bookmark twice with the same url.
neat little function