get_posts(), WP_Query or query_posts(): which one to use
WordPress has three ways to get posts: get_posts(), new WP_Query() and query_posts(). They accept the same arguments and end up in the same SQL, but they behave differently, and one of them should practically never be used. Choosing the right one avoids broken pagination and posts that “leak” into other parts of the page.
Problem: A theme needs secondary lists of posts (a sidebar, related posts, a shortcode), and all three functions appear in tutorials and plugins.
Solution: Use get_posts() for simple lists, new WP_Query() when you need the loop functions or pagination, and pre_get_posts to change the main query of a page. Do not use query_posts().
get_posts(): a plain array
<?php
$recent = get_posts( array(
'posts_per_page' => 5,
) );
foreach ( $recent as $recent_post ) {
printf(
'<li><a href="%s">%s</a></li>',
esc_url( get_permalink( $recent_post ) ),
esc_html( get_the_title( $recent_post ) )
);
}
It returns an array of WP_Post objects and has different defaults than WP_Query: no_found_rows and ignore_sticky_posts are true (no total count, no sticky posts on top) and suppress_filters is true, so query filters of plugins do not apply. Functions that take a post (get_permalink( $post ), get_the_title( $post )) work without touching the global $post.
If you want template tags like the_title(), use the global variable and reset it afterwards:
<?php
global $post;
foreach ( $recent as $post ) {
setup_postdata( $post );
the_title( '<h3>', '</h3>' );
}
wp_reset_postdata();
WP_Query: the loop and pagination
<?php
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 10,
'paged' => max( 1, get_query_var( 'paged' ) ),
) );
if ( $events->have_posts() ) {
while ( $events->have_posts() ) {
$events->the_post();
the_title( '<h3>', '</h3>' );
}
echo paginate_links( array( 'total' => $events->max_num_pages ) );
wp_reset_postdata();
}
The object knows the total number of results (found_posts, max_num_pages), so it is the choice for paginated lists and for anything that needs have_posts() / the_post().
query_posts(): do not use it
query_posts() replaces the main query of the page with a new one after WordPress has already run it. The page is queried twice, conditional tags and pagination still refer to the original request, and /page/2/ often shows a 404 or the wrong posts.
To show 5 posts per page on the home page, change the main query before it runs:
<?php
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
$query->set( 'posts_per_page', 5 );
}
} );
More about this in changing the arguments of the global query; the classic pagination bug it causes is described in the second page shows the posts of the first one.
NOTE: After every custom loop that calls the_post() or setup_postdata(), call wp_reset_postdata(). Without it the global $post stays on the last post of your list, and the comments, the sidebar and the footer of the page work with the wrong post.