wp_list_pluck

The timeline below displays how wordpress function wp_list_pluck has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.2

/**
 * Plucks a certain field out of each object or array in an array.
 *
 * This has the same functionality and prototype of
 * array_column() (PHP 5.5) but also supports objects.
 *
 * @since 3.1.0
 * @since 4.0.0 $index_key parameter added.
 * @since 4.7.0 Uses `WP_List_Util` class.
 *
 * @param array      $input_list List of objects or arrays.
 * @param int|string $field      Field from the object to place instead of the entire object.
 * @param int|string $index_key  Optional. Field from the object to use as keys for the new array.
 *                               Default null.
 * @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.
 */
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);
}

WordPress Version: 6.1

/**
 * Plucks a certain field out of each object or array in an array.
 *
 * This has the same functionality and prototype of
 * array_column() (PHP 5.5) but also supports objects.
 *
 * @since 3.1.0
 * @since 4.0.0 $index_key parameter added.
 * @since 4.7.0 Uses `WP_List_Util` class.
 *
 * @param array      $list      List of objects or arrays.
 * @param int|string $field     Field from the object to place instead of the entire object.
 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
 *                              Default null.
 * @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.
 */
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);
}

WordPress Version: 5.9

/**
 * Plucks a certain field out of each object or array in an array.
 *
 * This has the same functionality and prototype of
 * array_column() (PHP 5.5) but also supports objects.
 *
 * @since 3.1.0
 * @since 4.0.0 $index_key parameter added.
 * @since 4.7.0 Uses `WP_List_Util` class.
 *
 * @param array      $list      List of objects or arrays.
 * @param int|string $field     Field from the object to place instead of the entire object.
 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
 *                              Default null.
 * @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.
 */
function wp_list_pluck($list, $field, $index_key = null)
{
    $util = new WP_List_Util($list);
    return $util->pluck($field, $index_key);
}

WordPress Version: 5.8

/**
 * 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.
 *
 * @since 3.1.0
 * @since 4.0.0 $index_key parameter added.
 * @since 4.7.0 Uses `WP_List_Util` class.
 *
 * @param array      $list      List of objects or arrays.
 * @param int|string $field     Field from the object to place instead of the entire object.
 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
 *                              Default null.
 * @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.
 */
function wp_list_pluck($list, $field, $index_key = null)
{
    $util = new WP_List_Util($list);
    return $util->pluck($field, $index_key);
}

WordPress Version: 5.1

/**
 * 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.
 *
 * @since 3.1.0
 * @since 4.0.0 $index_key parameter added.
 * @since 4.7.0 Uses `WP_List_Util` class.
 *
 * @param array      $list      List of objects or arrays
 * @param int|string $field     Field from the object to place instead of the entire object
 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
 *                              Default null.
 * @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.
 */
function wp_list_pluck($list, $field, $index_key = null)
{
    $util = new WP_List_Util($list);
    return $util->pluck($field, $index_key);
}

WordPress Version: 4.7

/**
 * 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.
 *
 * @since 3.1.0
 * @since 4.0.0 $index_key parameter added.
 * @since 4.7.0 Uses WP_List_Util class.
 *
 * @param array      $list      List of objects or arrays
 * @param int|string $field     Field from the object to place instead of the entire object
 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
 *                              Default null.
 * @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.
 */
function wp_list_pluck($list, $field, $index_key = null)
{
    $util = new WP_List_Util($list);
    return $util->pluck($field, $index_key);
}

WordPress Version: 4.2

/**
 * 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.
 *
 * @since 3.1.0
 * @since 4.0.0 $index_key parameter added.
 *
 * @param array      $list      List of objects or arrays
 * @param int|string $field     Field from the object to place instead of the entire object
 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
 *                              Default null.
 * @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.
 */
function wp_list_pluck($list, $field, $index_key = null)
{
    if (!$index_key) {
        /*
         * This is simple. Could at some point wrap array_column()
         * if we knew we had an array of arrays.
         */
        foreach ($list as $key => $value) {
            if (is_object($value)) {
                $list[$key] = $value->{$field};
            } else {
                $list[$key] = $value[$field];
            }
        }
        return $list;
    }
    /*
     * When index_key is not set for a particular item, push the value
     * to the end of the stack. This is how array_column() behaves.
     */
    $newlist = array();
    foreach ($list as $value) {
        if (is_object($value)) {
            if (isset($value->{$index_key})) {
                $newlist[$value->{$index_key}] = $value->{$field};
            } else {
                $newlist[] = $value->{$field};
            }
        } else if (isset($value[$index_key])) {
            $newlist[$value[$index_key]] = $value[$field];
        } else {
            $newlist[] = $value[$field];
        }
    }
    return $newlist;
}

WordPress Version: 4.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.
 *
 * @since 3.1.0
 * @since 4.0.0 $index_key parameter added.
 *
 * @param array      $list      List of objects or arrays
 * @param int|string $field     Field from the object to place instead of the entire object
 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
 *                              Default null.
 * @return array Array of found values. If $index_key is set, an array of found values with keys
 *               corresponding to $index_key.
 */
function wp_list_pluck($list, $field, $index_key = null)
{
    if (!$index_key) {
        /*
         * This is simple. Could at some point wrap array_column()
         * if we knew we had an array of arrays.
         */
        foreach ($list as $key => $value) {
            if (is_object($value)) {
                $list[$key] = $value->{$field};
            } else {
                $list[$key] = $value[$field];
            }
        }
        return $list;
    }
    /*
     * When index_key is not set for a particular item, push the value
     * to the end of the stack. This is how array_column() behaves.
     */
    $newlist = array();
    foreach ($list as $value) {
        if (is_object($value)) {
            if (isset($value->{$index_key})) {
                $newlist[$value->{$index_key}] = $value->{$field};
            } else {
                $newlist[] = $value->{$field};
            }
        } else if (isset($value[$index_key])) {
            $newlist[$value[$index_key]] = $value[$field];
        } else {
            $newlist[] = $value[$field];
        }
    }
    return $newlist;
}

WordPress Version: 3.7

/**
 * Pluck a certain field out of each object in a list.
 *
 * @since 3.1.0
 *
 * @param array $list A list of objects or arrays
 * @param int|string $field A field from the object to place instead of the entire object
 * @return array
 */
function wp_list_pluck($list, $field)
{
    foreach ($list as $key => $value) {
        if (is_object($value)) {
            $list[$key] = $value->{$field};
        } else {
            $list[$key] = $value[$field];
        }
    }
    return $list;
}