wp_list_pluck( array $input_list, int|string $field, int|string $index_key = null ): array
- Since
- 3.1.0, 4.0.0, 4.7.0
- Source
wp-includes/functions.php:5395
Extracts one field from every object or array in a list, returning a flat array of just those values. Pass $index_key to re-key the result by a second field instead of keeping the original list's keys. It works on arrays of stdClass objects, such as get_posts() or get_terms() results, as well as associative arrays, which is what sets it apart from PHP's native array_column(). If $input_list is not an array, the function quietly returns an empty array rather than throwing a warning.
Description
This has the same functionality and prototype of array_column() (PHP 5.5) but also supports objects.
Compatibility
- WordPress
- since 4.7.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$input_listarray- List of objects or arrays.
$fieldint|string- Field from the object to place instead of the entire object.
$index_keyint|stringoptional- Field from the object to use as keys for the new array.
Default null.Default:null
Return value
array- Array of found values. If
$index_keyis set, an array of found values with keys corresponding to$index_key. If$index_keyis null, array keys from the original$input_listwill be preserved in the results.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Get post titles keyed by post ID
Fetch a batch of posts and reduce them to a simple ID-to-title map for a dropdown or debug dump.
$posts = get_posts( array(
'post_type' => 'post',
'posts_per_page' => 5,
) );
$titles_by_id = wp_list_pluck( $posts, 'post_title', 'ID' );
echo '<pre>' . esc_html( print_r( $titles_by_id, true ) ) . '</pre>';get_posts() returns WP_Post objects, and wp_list_pluck() reads their properties directly without any get_ prefix.
Build a lookup table of category names by term ID
Pull all categories and turn them into a term_id-to-name array for quick lookups elsewhere in a template.
$categories = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
$names_by_id = wp_list_pluck( $categories, 'name', 'term_id' );
echo '<pre>' . esc_html( print_r( $names_by_id, true ) ) . '</pre>';get_terms() can return a WP_Error on failure, so check is_wp_error() before passing its result to wp_list_pluck() in real code.
Common problems and fixes · 4
- Why does wp_list_pluck() give me back an empty array?
- Why did my array keys change after calling wp_list_pluck()?
- Can I lose entries when using an $index_key that isn't unique?
- Does wp_list_pluck() work if some items are objects and others are arrays?
Why does wp_list_pluck() give me back an empty array?
Why did my array keys change after calling wp_list_pluck()?
Can I lose entries when using an $index_key that isn't unique?
Does wp_list_pluck() work if some items are objects and others are arrays?
Alternatives and related functions
array_column- When every item in the list is already a plain array (not an object) and you're fine with the native PHP function's behavior.
wp_list_filter- When you need to narrow the list down to matching items instead of extracting one field from every item.
wp_list_sort- When you need the objects or arrays themselves, sorted by a field, rather than a flat list of that field's values.
WP_List_Util- When you need to pluck and filter or sort the same list in one pass, since wp_list_pluck() itself is just a thin wrapper around this class.
Performance profile
How much work a call to wp_list_pluck() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 6–14
- Plugin surface
- None
- Called by
- 44
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 15.
Nothing here hands control to plugin code.
44 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_list_pluck() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_array($input_list) | 6 | none |
is_array($input_list) | 14 | ->pluck() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 15 instructions, 6–14 executed per call, 1 branch. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- WP_List_Util::__construct()Constructor.
Used by · 44
- WP_Automatic_Updater::send_debug_email()Prepares and sends an email of a full log of background update results, useful for debugging and geekery.
- WP_Comment_Query::fill_descendants()Fetch descendants for located comments.
- WP_Comments_List_Table::prepare_items()Prepares the comments list items.
- WP_Community_Events::trim_events()Prepares the event list for presentation.
- WP_Customize_Manager::import_theme_starter_content()Imports theme starter content into the customized state.
- WP_Customize_Manager::unsanitized_post_values()Gets dirty pre-sanitized setting values in the current customized state.
- WP_Media_List_Table::display_rows()Generates the list table rows.
- WP_Plugin_Install_List_Table::prepare_items()
- WP_Post::__get()Getter.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_REST_Menu_Items_Controller::prepare_item_for_response()Prepares a single nav menu item output for response.
- WP_REST_Post_Search_Handler::search_items()Searches posts for a given search request.
Show all 44
- WP_REST_Post_Types_Controller::prepare_item_for_response()Prepares a post type object for serialization.
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- WP_REST_Term_Search_Handler::search_items()Searches terms for a given search request.
- WP_Script_Modules::get_dependents()Gets all dependents of a script module.
- WP_Tax_Query::transform_query()Transforms a single query, from one field to another.
- WP_Term_Query::get_terms()Retrieves the query results.
- WP_Textdomain_Registry::invalidate_mo_files_cache()Invalidate the cache for .mo files.
- WP_Widget_Media::form()Outputs the settings update form.
- WP_Widget_Media::widget()Displays the widget on the front-end.
- WP_Widget_Media_Audio::render_media()Render the media on the frontend.
- WP_Widget_Media_Gallery::render_media()Render the media on the frontend.
- WP_Widget_Media_Image::render_media()Render the media on the frontend.
- WP_Widget_Media_Video::render_media()Render the media on the frontend.
- WP_Widget_Recent_Comments::widget()Outputs the content for the current Recent Comments widget instance.
- _default_wp_die_handler()Kills WordPress execution and displays HTML page with an error message.
- get_block_templates()Retrieves a list of unified template objects based on a query.
- get_feed_build_date()Gets the UTC time of the most recently modified post from WP_Query.
- get_inline_data()Adds hidden fields with the data for use in the inline editor for posts and pages.
- get_media_states()Retrieves an array of media states from an attachment.
- get_post_galleries()Retrieves galleries from the passed post's content.
- get_post_galleries_images()Retrieves the image srcs from galleries from a post's content, if present.
- get_terms_to_edit()Gets comma-separated list of terms available to edit for the given post ID.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
- is_object_in_term()Determines if the given object is associated with any of the given terms.
- render_block_core_latest_comments()Renders the `core/latest-comments` block on server.
- update_post_author_caches()Updates post author user caches for a list of post objects.
- update_post_parent_caches()Updates parent post caches for a list of post objects.
- wp_delete_term()Removes a term from the database.
- wp_enqueue_media()Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs.
- wp_filter_oembed_iframe_title_attribute()Filters the given oEmbed HTML to make sure iframes have a title attribute.
- wp_insert_term()Adds a new term to the database.
- wp_update_post()Updates a post with new post data.
Source code
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 );}Changelog
Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
WP_List_Util class.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.