WordPress: Useful functions for working with arrays and objects

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
Search for a command to run...

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
I would love to see more high quality articles like this one with super useful php functions 👍
Magnific article 😃!!!!
Recently, a friend asked me to send a script to remove pre tags from a string, including content. He uses a script to calculate the reading time for the content, but doesn't want the pre-tags to be included in the scoring. So if you ever have a simil...

When there are problems with one's WordPress website, it happens that one has to create a user without having access to the database or the WordPress admin. Then it is useful to be able to create or log in a user using FTP. So here you will learn how...

Occasionally it happens that overlaps occur in a large table and the message Duplicate entry '0' for key 'PRIMARY' for query then gets the upper hand in the error logs. This error can be fixed quite easily with a short line of SQL. Fix duplicate entr...

As a rule, searches are created with the MySQL LIKE. This usually works very well, as long as the database table is not too large. However, if you work with a large database (millions+ entries), then you quickly notice how long a search in it actuall...

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.
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',
* ]
*/
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',
* ]
*/
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_uniqueand thenarray_filterto 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 ]
*/
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,
* ],
* ]
*/