WordPress Array Helpers: wp_list_pluck, wp_list_filter, and wp_list_sort for Object Arrays

WordPress core uses arrays of objects and associative arrays everywhere: get_posts() returns an array of WP_Post objects, get_users() returns an array of WP_User objects, get_terms() returns an array of WP_Term objects, and many internal registries store arrays of associative arrays. Extracting a single field from all items in such an array, or filtering the array to only items matching a condition, is a repetitive task that is easy to get wrong with manual foreach loops. WordPress provides three utility functions for working with these arrays: wp_list_pluck() extracts one field from every item, wp_list_filter() filters items by matching field values, and wp_list_sort() sorts the array by a field. All three work correctly with both arrays of objects (accessing public properties) and arrays of associative arrays (accessing keys), making them versatile tools for any WordPress data manipulation task.

Problem: You have an array of WP_Post objects from get_posts() and need to: extract just the post IDs into a flat array, filter the list to only posts with a specific post format, and sort the remainder by title alphabetically — all without writing manual loops.

Solution: Chain wp_list_pluck(), wp_list_filter(), and wp_list_sort() to transform the array cleanly in a few readable lines.

<?php
$posts = get_posts( [
    'post_type'      => 'post',
    'posts_per_page' => 50,
    'post_status'    => 'publish',
] );

// ── wp_list_pluck() — extract one field from every item ───────────────
// Works with objects (public properties) and associative arrays (keys)

$ids     = wp_list_pluck( $posts, 'ID' );          // [1, 5, 42, 87, ...]
$titles  = wp_list_pluck( $posts, 'post_title' );  // ['Hello World', ...]
$authors = wp_list_pluck( $posts, 'post_author' ); // ['3', '3', '1', ...]

// With index_key: use a field as the array key in the result
$ids_by_name = wp_list_pluck( $posts, 'ID', 'post_name' );
// ['hello-world' => 1, 'my-post' => 5, ...]

// Useful for building dropdown options from terms:
$terms = get_terms( [ 'taxonomy' => 'category' ] );
$term_options = wp_list_pluck( $terms, 'name', 'term_id' );
// [3 => 'Uncategorized', 7 => 'WordPress', ...]

// ── wp_list_filter() — keep only items matching conditions ────────────
// Find all posts by author ID 3:
$author_posts = wp_list_filter( $posts, [ 'post_author' => '3' ] );

// Exclude a specific status (operator: NOT):
$non_sticky = wp_list_filter( $posts, [ 'post_status' => 'future' ], 'NOT' );

// Match any of multiple values (operator: OR):
$featured = wp_list_filter( $posts, [
    'post_status' => 'publish',
    'post_author' => '1',
], 'OR' ); // posts that are published OR by author 1

// ── wp_list_sort() — sort an array of objects/arrays by a field ───────
// Sort posts by post_title alphabetically (ascending):
$sorted = wp_list_sort( $posts, 'post_title', 'ASC' );

// Sort by date, newest first:
$sorted_by_date = wp_list_sort( $posts, 'post_date', 'DESC' );

// Sort associative array by a key:
$widgets = [
    [ 'id' => 'text-1',   'order' => 3 ],
    [ 'id' => 'search-1', 'order' => 1 ],
    [ 'id' => 'meta-1',   'order' => 2 ],
];
$sorted_widgets = wp_list_sort( $widgets, 'order', 'ASC', true ); // true = preserve keys

NOTE: wp_list_sort() modifies the array in-place (by reference) and also returns the sorted array — so either assign the return value or rely on the pass-by-reference behaviour. wp_list_filter()'s operator parameter accepts 'AND' (default — all conditions must match) or 'NOT' (exclude items matching any condition) or 'OR' (include items matching any condition). For complex filtering logic that wp_list_filter() cannot express, fall back to array_filter() with a closure. wp_list_pluck() silently returns an empty string for missing properties or keys — so it is safe on mixed arrays where not every item has the requested field.