wp_list_pluck();
Quite often, I use an ACF Post Object field to let the client chose a multiple number of items. That returns an array of post objects. If I want to get the ID for each of those post objects, you could loop over it. Or, let WP abstract that away with a little helper function. Enter wp_list_pluck()
.
// $post_objects is our array of WP post objects
$post_ids = wp_list_pluck($post_objects, 'ID');
We now have $post_ids
which only contains post ID’s. This is useful in a multitude of ways, like if you need to put them back into a WP_Query
or use those ID’s in some other way.
Array (
[0] => 116
[1] => 433
[2] => 119
)
That can save a few lines of foreach
loops, can’t it. Cleaner code is always nice.