wp_list_pluck() WordPress Function

The wp_list_pluck() function is used to retrieve a list of values from an array of objects. The function takes an array of objects and a key name as arguments, and returns an array of values for the specified key name.

wp_list_pluck( array $list, int|string $field, int|string $index_key = null ) #

Plucks a certain field out of each object or array in an array.


Description

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


Top ↑

Parameters

$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)(Optional) Field from the object to use as keys for the new array.

Default value: null


Top ↑

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 $list will be preserved in the results.


Top ↑

Source

File: wp-includes/functions.php

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

	$util = new WP_List_Util( $list );

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


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More