wp_list_pluck(), wp_list_filter() and wp_list_sort(): array helpers in WordPress
WordPress returns lists everywhere: get_posts() gives WP_Post objects, get_terms() gives WP_Term objects, get_users() gives WP_User objects. Getting one field from all of them, keeping some of them or sorting them by a field are everyday tasks, and core has three small functions for them that work with lists of objects and lists of arrays alike.
Problem: From a list of posts you need just the IDs, only the posts of one author, and the rest sorted by title, and every foreach loop for it looks the same.
Solution: Use wp_list_pluck() to extract one field, wp_list_filter() to keep the items whose fields match, and wp_list_sort() (WordPress 4.7+) to sort by one or more fields.
wp_list_pluck(): one field from every item
<?php $posts = get_posts( array( 'posts_per_page' => 50 ) ); $ids = wp_list_pluck( $posts, 'ID' ); // array( 12, 5, 42, ... ) $titles = wp_list_pluck( $posts, 'post_title' ); // With a third argument, another field becomes the key $id_by_slug = wp_list_pluck( $posts, 'ID', 'post_name' ); // array( 'hello-world' => 12, ... ) // Options for a select field from terms $terms = get_terms( array( 'taxonomy' => 'category', 'hide_empty' => false ) ); $options = wp_list_pluck( $terms, 'name', 'term_id' ); // array( 3 => 'News', 7 => 'WordPress' )
Every item must have the field; a missing property triggers a notice. If you only need IDs, it is cheaper to ask the query for them: get_posts( array( 'fields' => 'ids' ) ).
wp_list_filter(): keep items by field values
<?php // Posts of author 3 (post_author is a string in WP_Post) $by_author = wp_list_filter( $posts, array( 'post_author' => '3' ) ); // AND (default): all conditions must match $mine_open = wp_list_filter( $posts, array( 'post_author' => '3', 'comment_status' => 'open' ) ); // OR: at least one condition matches $either = wp_list_filter( $posts, array( 'post_author' => '3', 'comment_status' => 'open' ), 'OR' ); // NOT: none of the conditions matches $others = wp_list_filter( $posts, array( 'post_author' => '3' ), 'NOT' );
The comparison is loose (==), and only exact values are supported. For anything else, such as “more than 10 comments”, use array_filter() with a callback. The array keys of the original list are kept, so use array_values() if you need a list from 0.
wp_list_sort(): sort by a field
<?php
$by_title = wp_list_sort( $posts, 'post_title', 'ASC' );
$newest = wp_list_sort( $posts, 'post_date', 'DESC' );
// Several fields: by menu order, then by title
$ordered = wp_list_sort( $posts, array( 'menu_order' => 'ASC', 'post_title' => 'ASC' ) );
// Arrays work the same way; true keeps the keys
$widgets = array(
'text-1' => array( 'order' => 3 ),
'search-1' => array( 'order' => 1 ),
);
$sorted = wp_list_sort( $widgets, 'order', 'ASC', true );
wp_list_sort() returns a new sorted array and does not change the original one, so always use the return value.
NOTE: These helpers work on data that is already loaded. If the list comes from the database, filtering and sorting in the query (author, orderby, meta_query) is faster and loads fewer rows; use the helpers for lists you already have, such as terms of a post or results combined from several queries.