get_posts( array $args = null ): WP_Post[]|int[]
- Since
- 1.2.0
- Source
wp-includes/post.php:2543
Retrieve an array of posts matching your criteria with get_posts(), a WP_Query wrapper that returns post objects without pagination overhead. It defaults to the 5 latest posts and accepts every WP_Query argument plus aliases like numberposts, category, include, and exclude. Sticky handling and found-rows counting are always disabled, so use WP_Query directly when you need pagination.
Description
For more information on the accepted arguments, see the https://developer.wordpress.org/reference/classes/wp_query/ WP_Query documentation in the Developer Handbook.
The $ignore_sticky_posts and $no_found_rows arguments are ignored by this function and both are set to true.
The defaults are as follows:
Compatibility
- WordPress
- since 1.2.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
$argsarrayoptional- Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments.Default:
null$numberpostsintdefault: 5Total number of posts to retrieve. Is an alias of$posts_per_pagein WP_Query. Accepts -1 for all.$categoryint|stringdefault: 0Category ID or comma-separated list of IDs (this or any children). Is an alias of$catin WP_Query.$includeint[]default: empty arrayAn array of post IDs to retrieve, sticky posts will be included. Is an alias of$post__inin WP_Query.$excludeint[]default: empty arrayAn array of post IDs not to retrieve.$suppress_filtersbooldefault: trueWhether to suppress filters.
Return value
WP_Post[]|int[]- Array of post objects or post IDs.
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.
List the five latest posts
Switch the sandbox below to see the same call against a custom post type.
$latest = get_posts( array(
'numberposts' => 5,
'post_type' => post_type_exists( 'book' ) ? 'book' : 'post',
) );
if ( ! $latest ) {
echo 'Nothing matched.';
}
foreach ( $latest as $item ) {
echo '- ', get_the_title( $item ), ' (', $item->post_type, ")\n";
}post_type_exists() is only here so the one snippet works in both sandboxes.
Order results by a numeric custom field
Combining meta_key with orderby meta_value_num sorts by the field and, as a side effect, excludes posts that do not have it.
$priced = get_posts( array(
'post_type' => 'post',
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'numberposts' => 5,
) );
if ( ! $priced ) {
echo 'No posts carry a price.';
}
foreach ( $priced as $item ) {
echo get_the_title( $item ), ' costs ', get_post_meta( $item->ID, 'price', true ), "\n";
}Use meta_value_num rather than meta_value, or 100 sorts before 9.
Performance profile
How much work a call to get_posts() 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
- Heavy
- Scaling
- Constant
- Instructions
- 28–53
- Plugin surface
- None
- Called by
- 27
Reaches the database via ->query().
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 63.
Nothing here hands control to plugin code.
27 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_posts()this function does it - sqldatabase query
->query()called directly
Further down the call graph this can also reach hook. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
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 get_posts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($parsed_args) | 28–43 | wp_parse_args(), ->query() |
!empty($parsed_args) | 35–53 | wp_parse_args(), wp_parse_id_list(), ->query() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 63 instructions, 28–53 executed per call, 7 branches. 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 · 3
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_parse_id_list()Cleans up an array, comma- or space-separated list of IDs.
- WP_Query::__construct()Constructor.
Used by · 27
- Featured_Content::get_featured_post_ids()Get featured post IDs
- Featured_Content::get_featured_posts()Get featured posts.
- Twenty_Fourteen_Ephemera_Widget::widget()Output the HTML for this widget.
- WP_Customize_Manager::get_changeset_posts()Gets changeset posts.
- WP_Customize_Nav_Menus::load_available_items_query()Performs the post_type and taxonomy queries for loading available menu items.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- do_all_enclosures()Performs all enclosures.
- do_all_pingbacks()Performs all pingbacks.
- do_all_trackbacks()Performs all trackbacks.
- gallery_shortcode()Builds the Gallery shortcode output.
- get_boundary_post()Retrieves the boundary post.
- get_children()Retrieves all children of the post parent ID.
Show all 27
- get_uploaded_header_images()Gets the header images uploaded for the active theme.
- twentyeleven_get_gallery_images()Retrieve the IDs for images in a gallery.
- twentyfourteen_the_attached_image()Print the attached image with a link to the next attached image.
- twentyten_get_gallery_images()Retrieve the IDs for images in a gallery.
- twentythirteen_the_attached_image()Print the attached image with a link to the next attached image.
- wp_add_trashed_suffix_to_post_name_for_trashed_posts()Adds a suffix if any trashed posts have a given slug.
- wp_ajax_find_posts()Handles querying posts for the Find Posts modal via AJAX.
- wp_dashboard_recent_drafts()Show recent drafts of the user on the dashboard.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- wp_get_nav_menu_items()Retrieves all menu items of a navigation menu.
- wp_get_recent_posts()Retrieves a number of recent posts.
- wp_nav_menu_item_post_type_meta_box()Displays a meta box for a post type menu item.
- wp_playlist_shortcode()Builds the Playlist shortcode output.
- wp_xmlrpc_server::wp_getMediaLibrary()Retrieves a collection of media library items (or attachments).
- wp_xmlrpc_server::wp_getPages()Retrieves Pages.
Source code
function get_posts( $args = null ) { $defaults = array( 'numberposts' => 5, 'category' => 0, 'orderby' => 'date', 'order' => 'DESC', 'include' => array(), 'exclude' => array(), 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', 'suppress_filters' => true, ); $parsed_args = wp_parse_args( $args, $defaults ); if ( empty( $parsed_args['post_status'] ) ) { $parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish'; } if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) { $parsed_args['posts_per_page'] = $parsed_args['numberposts']; } if ( ! empty( $parsed_args['category'] ) ) { $parsed_args['cat'] = $parsed_args['category']; } if ( ! empty( $parsed_args['include'] ) ) { $incposts = wp_parse_id_list( $parsed_args['include'] ); $parsed_args['posts_per_page'] = count( $incposts ); // Only the number of posts included. $parsed_args['post__in'] = $incposts; } elseif ( ! empty( $parsed_args['exclude'] ) ) { $parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] ); } $parsed_args['ignore_sticky_posts'] = true; $parsed_args['no_found_rows'] = true; $get_posts = new WP_Query(); return $get_posts->query( $parsed_args );}Changelog
Introduced in 1.2.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$args retyped from array to array|string.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/post.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.