wp_list_pluck()WP 3.1.0

Pluck a certain field out of each object in a list.

This has the same functionality and prototype of array_column() (PHP 5.5) but also supports objects.

1 time — 0.00001 sec (speed of light) | 50000 times — 0.45 sec (very fast)

No Hooks.

Return

Array. Array of found values. If $index_key is set, an array of found values with keys corresponding to $index_key. If $index_key is null, array keys from the original $input_list will be preserved in the results.

Usage

wp_list_pluck( $input_list, $field, $index_key );
$input_list(array) (required)
List of objects or arrays.
$field(int|string) (required)
Field from the object to place instead of the entire object.
$index_key(int|string)
Field from the object to use as keys for the new array.
Default: null

Examples

0

#1 Example of operation wp_list_pluck()

This example shows how the function collects nested array fields. Suppose we have an array of arrays from which we need to get all the values of the name field:

// Available array
$foods = array(

	6 => array(
		'name' => 'Banana',
		'color' => 'Yellow',
	),
	9 => array(
		'name' => 'Kiwi',
		'color' => 'Green',
	),
	1 => array(
		'name' => 'Mandarin',
		'color' => 'Orange',
	),
	3 => array(
		'name' => 'Apple',
		'color' => 'Red',
	),
);

// Gather all the fields
$food_names = wp_list_pluck( $foods, 'name' );

/* $food_names will contain:
Array
(
	[6] => Banana
	[9] => Kiwi
	[1] => Mandarin
	[3] => Apple
)
*/

// If you specify the third parameter
$food_names = wp_list_pluck( $foods, 'name', 'color' );

/* $food_names will contain:
Array
(
	[Yellow] => Banana
	[Green] => Kiwi
	[Orange] => Tangerine
	[Red] => Apple
)
*/
0

#2 Working with objects

This example is more relevant to WP and shows how to collect all the post titles retrieved via get_posts() in one array:

// Get the posts
$posts = get_posts();

// Collect post_title fields into an array
$posts_titles = wp_list_pluck( $posts, 'post_title' );

/* $posts_titles contains a array:
Array
(
	[0] => 10 premium themes for online store on WordPress
	[1] => Monitoring non-existent pages (plugin "404 Error Logger")
	[2] => WP-Cumulus: 3D tag cloud on flash (improved Russian version)
	[3] => 10 ways to change the RSS feed in WordPress
	[4] => Webmaster Yandex plugin for Wordpress
)
*/
0

#3 Specifying a key from a nested array

Suppose we have such an array:

$foods = array(
	array(
		'id'  => 4,
		'name'  => 'Banana',
		'color' => 'Yellow',
	),
	array(
		'id'  => '5',
		'name'  => 'Apple',
		'color' => 'Red',
	),
	array(
		'id'  => 2,
		'name'  => 'Lettuce',
		'color' => 'Green',
	),
	array(
		'id'  => '7',
		'name'  => 'Apple',
		'color' => 'Red',
	),
);

And we need to get all the name fields and in the final array as keys we need to use the id field:

$food_names = wp_list_pluck( $foods, 'name', 'id' );

/* we get:
array(
	4 => 'Banana',
	5 => 'Apple',
	2 => 'Lettuce',
	7 => 'Apple'
);
*/

Changelog

Since 3.1.0 Introduced.
Since 4.0.0 $index_key parameter added.
Since 4.7.0 Uses WP_List_Util class.

wp_list_pluck() code WP 6.4.3

function wp_list_pluck( $input_list, $field, $index_key = null ) {
	if ( ! is_array( $input_list ) ) {
		return array();
	}

	$util = new WP_List_Util( $input_list );

	return $util->pluck( $field, $index_key );
}