Using wp_list_pluck to extract values from object arrays in WordPress
The wp_list_pluck
function is a feature offered by WordPress that allows users to retrieve a specific field’s value from an array or object. This function operates by searching through the array or object for the specified field and then creating a new array composed of the values found in that field.
One of the main uses of the wp_list_pluck
function is to simplify the process of extracting certain data from a complex or multi-dimensional array or object. This can be particularly applicable in situations where the user needs to gather data from a specific field across a large or complicated array or object.
While the wp_list_pluck
function does not modify the original array or object, it does create a new array based on the specified field’s values. This can make it easier to handle or manipulate the data in question, as the user can work with a simpler and more streamlined array.
Parameters Accepted by the wp_list_pluck Function
The wp_list_pluck
function in WordPress is designed to accept three parameters. These parameters are as follows:
$input_list
(array): This is a mandatory parameter. It refers to the list of objects or arrays that the function will process.$field
(int|string): This is also a required parameter. It specifies the field from the object that will be used in place of the entire object.$index_key
(int|string): This is an optional parameter with a default value of null. It defines the field from the object that will be used as keys for the new array that the function will create.
Return Value of the wp_list_pluck Function
The wp_list_pluck
function returns an array of found values from the processed list of objects or arrays. If the $index_key
parameter is set, the function will return an array of found values, with the keys corresponding to the $index_key
. If the $index_key
is not set or is null, the function will preserve the array keys from the original $input_list
in the results.
Examples
How to Extract Post Titles from an Array of Post Objects
The wp_list_pluck()
function is commonly used to extract a certain field’s values from an array of objects or arrays. In this example, we will use it to get all post titles from an array of post objects.
$posts = get_posts(['numberposts' => -1]);
$post_titles = wp_list_pluck($posts, 'post_title');
In the above code, get_posts()
function is used to retrieve all posts. The wp_list_pluck()
function then extracts the ‘post_title’ field from each post object in the array.
How to Extract User Emails from an Array of User Objects
The wp_list_pluck()
function can be used to extract a specific field from an array of objects. In this example, we’ll use it to extract all user emails from an array of user objects.
$users = get_users();
$user_emails = wp_list_pluck($users, 'user_email');
Here, the get_users()
function is used to retrieve all users. The wp_list_pluck()
function then extracts the ‘user_email’ field from each user object in the array.
How to Extract Categories Names from an Array of Category Objects
The wp_list_pluck()
function can also be used to extract a specific field from an array of term objects. In this example, we’ll use it to extract all category names from an array of category objects.
$categories = get_categories();
$category_names = wp_list_pluck($categories, 'name');
In this code, the get_categories()
function is used to retrieve all categories. The wp_list_pluck()
function then extracts the ‘name’ field from each category object in the array.
Conclusion
The wp_list_pluck
function in WordPress is a utility that allows you to extract a single field’s value from an array or object. It is particularly useful when you need to retrieve a specific value from a multi-dimensional array or a list of objects. The function takes two parameters: the array or list of objects you want to pluck from and the field you want to pluck. The function then returns an array of the plucked values. This function can be used in a variety of scenarios, such as retrieving a list of post IDs from a list of post objects, or extracting a list of user roles from a list of user objects.