get_posts() is a lightweight wrapper around WP_Query that returns an array of posts. If you searched for get_posts wordpress, wordpress get_posts, or wp get posts, this guide shows the exact syntax, the most useful args, and when to switch to WP_Query. If you need to get one post by ID, use get_post() (see the quick compare below).
How do I use WordPress get_posts()? Show the syntax and a basic example.
$args = [ 'post_type' => 'post', 'posts_per_page' => 10, 'post_status' => 'publish', ]; $posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post );
echo get_the_title( $post ) . "\n";
}
wp_reset_postdata();
get_posts() is (and isn’t)WP_Query; returns WP_Post objects (or IDs).posts_per_page=5, post_status='publish', orderby='date', order='DESC', suppress_filters=true.| Arg | Purpose |
|---|---|
post_type |
Post/page/CPT |
posts_per_page |
Limit count |
tax_query |
Filter by terms |
meta_query |
Filter by meta |
orderby/order |
Sort results |
fields |
'ids' only |
no_found_rows |
Speed (no totals) |
update_post_term_cache |
Off if unused |
update_post_meta_cache |
Off if unused |
$ids = get_posts([ 'post_type' => 'post', 'fields' => 'ids', 'numberposts' => 20, // alias of posts_per_page ]);
Tip: Need pagination or totals? Use WP_Query (below).
How do I filter get_posts() by taxonomy or meta and when should I use WP_Query instead?
tax_query)$args = [ 'post_type' => ['post','your_cpt'], 'tax_query' => [[ 'taxonomy' => 'category', 'field' => 'slug', // or 'term_id', 'name' 'terms' => ['featured','news'], 'operator' => 'IN', ]], 'posts_per_page' => 10, ]; $posts = get_posts( $args );
meta_query)$args = [ 'post_type' => 'product', 'meta_query' => [[ 'key' => '_stock_status', 'value' => 'instock', 'compare' => '=', ]], ]; $posts = get_posts( $args );
$args = [ 'post_type' => 'product', 'meta_key' => '_price', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'posts_per_page' => 12, ]; $posts = get_posts( $args );
$args = [ 'post_type' => 'post', 'posts_per_page' => 10, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'suppress_filters' => true, ]; $posts = get_posts( $args );
WP_Query insteadUse WP_Query for pagination (paged), complex loops, or when you need totals:
$paged = max( 1, get_query_var('paged') ); $q = new WP_Query([ 'post_type' => 'post', 'posts_per_page' => 10, 'paged' => $paged, 'tax_query' => [[ 'taxonomy' => 'category','field' => 'slug','terms' => ['featured'] ]], ]); if ( $q->have_posts() ) { while ( $q->have_posts() ) { $q->the_post(); the_title('<h2>','</h2>'); } wp_reset_postdata(); }
get_posts() vs get_post() (quick compare)| Function | Returns | Use case |
|---|---|---|
get_posts( $args ) |
Array / IDs | Lists, widgets, simple queries |
get_post( $id ) |
Single WP_Post |
Known post by ID/object |
Docs: get_posts() · WP_Query
How do I query a custom post type and filter by multiple taxonomy terms and a meta field?
$args = [ 'post_type' => 'portfolio', 'tax_query' => [ 'relation' => 'AND', [ 'taxonomy' => 'project_type', 'field' => 'slug', 'terms' => ['app','web'], // posts that are app OR web 'operator' => 'IN', ], [ 'taxonomy' => 'industry', 'field' => 'slug', 'terms' => ['healthcare'], 'operator' => 'IN', 'include_children' => true, ], ], 'meta_query' => [ [ 'key' => '_featured', 'value' => '1', 'compare' => '=', ], ], 'posts_per_page' => 12, ]; $posts = get_posts( $args );
Tip: Use relation in tax_query and meta_query for complex logic; set include_children when you expect child terms.
Can I paginate with get_posts()? I need page 2, 3, etc.
WP_Queryget_posts() doesn’t handle paged pagination well. Use WP_Query with the same args and add paged:
$paged = max( 1, get_query_var('paged') ); $q = new WP_Query( [ 'post_type' => 'post', 'posts_per_page' => 10, 'paged' => $paged, // ... your tax_query/meta_query here ... ] ); // loop... wp_reset_postdata();
Alternative: For simple “load more”, keep get_posts() and pass offset, but true numbered pagination = WP_Query.
How do I exclude certain posts and ignore sticky posts?
$args = [ 'post_type' => 'post', 'posts_per_page' => 8, 'post__not_in' => [12, 34, 56], // exclude these IDs 'ignore_sticky_posts' => true, // don't pin stickies ]; $posts = get_posts( $args );
Also useful: post__in to enforce a specific list/order (then set orderby => 'post__in').
WP Assistant is a free tool created by Atiba Software, a WordPress design and development company located in Nashville, TN. If you need more personalized WordPress assistance let us know, and we’ll get back to you ASAP!