Kevin Pliester
Pixelbart

Pixelbart

WordPress: Useful functions for working with arrays and objects

WordPress: Useful functions for working with arrays and objects

Kevin Pliester's photo
Kevin Pliester
·May 11, 2022·

3 min read

Table of contents

  • wp_parse_args
  • shortcode_atts
  • wp_list_pluck
  • wp_list_filter

WordPress is packed with useful features. It is easy to lose the overview. Here you will find a selection of functions that deal with arrays and objects. A small collection of useful functions that you can always use, if you know that they exist.

wp_parse_args

This function is used everywhere in WordPress and is excellent for your own work. It involves passing in an array of default values and an array of custom arguments. It returns an array with all arguments, overwriting the default values. You can also pass a query string if you ever work with URLs.

$args = [
    'posts_per_page' = 15,
    'fields' => 'ids',
];

$defaults = [
    'post_type' => 'post',
    'posts_per_page' => 10,
    'post_status' => 'publish',
];

$results = wp_parse_args($args, $defaults);

/**
 * [
 *   'post_type' => 'post',
 *   'posts_per_page' => 15,
 *   'post_status' => 'publish',
 *   'fields' => 'ids',
 * ]
 */

shortcode_atts

Similar to wp_parse_args, two arrays are passed in. However, only the arguments that are returned as default values are spit out. Mainly the function is used within shortcodes of WordPress, so that no forbidden arguments can be passed via shortcode. But it can also be used for your own purposes.

$args = [
    'posts_per_page' = 15,
    'fields' => 'ids',
];

$defaults = [
    'post_type' => 'post',
    'posts_per_page' => 10,
    'post_status' => 'publish',
];

$results = shortcode_atts($defaults, $args);

/**
 * [
 *   'post_type' => 'post',
 *   'posts_per_page' => 15,
 *   'post_status' => 'publish',
 * ]
 */

wp_list_pluck

Super super useful! Here you pass an array and a key name. The output will be a new array with the values of the given key. For example, if you have an array of post objects, you can use this to create an array that only uses the IDs. WordPress uses this function in pretty much all parts of the core.

Note: The array is not unique. You have to take care of that yourself, so that there are no duplicate entries. Use array_unique and then array_filter to correct the keys.

$posts = [
    [
        'ID' => 1,
        'name' => 'Ein schöner Name #1',
        'status' => 'publish',
    ],
    [
        'ID' => 2,
        'name' => 'Ein schöner Name #2',
        'status' => 'publish'
    ],
    [
        'ID' => 3,
        'name' => 'Ein schöner Name #3',
        'status' => 'draft',
    ],
];

$results = wp_list_pluck($posts, 'ID');

/**
 * [ 1, 2, 3 ]
 */

wp_list_filter

This can be used to filter an array. Here you return the array, a key and the corresponding value. The function will then return a new array with only the elements matching the given key/value pair or the value you specified. The last parameter specifies how something is filtered. You can also specify OR, which is useful if you have set multiple filter pairs and want one of those filters to match, but not necessarily all at once with AND.

$posts = [
    [
        'ID' => 1,
        'name' => 'Ein schöner Name #1',
        'status' => 'publish',
    ],
    [
        'ID' => 2,
        'name' => 'Ein schöner Name #2',
        'status' => 'publish'
    ],
    [
        'ID' => 3,
        'name' => 'Ein schöner Name #3',
        'status' => 'draft',
    ],
];

$filter = ['status' => 'publish'];
$operator = 'AND';
$results = wp_list_filter($posts, $filter, $operator);

/**
 * [
 *   [
 *     'ID' => 1,
 *     'name' => 'Ein schöner Name #1',
 *     'status' => 'publish,
 *   ],
 *   [
 *     'ID' => 2,
 *     'name' => 'Ein schöner Name #2',
 *     'status' => 'publish,
 *   ],
 * ]
 */

Did you find this article valuable?

Support Kevin Pliester by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this