There are the following ways of getting a collection of posts in WordPress:
query_posts()(but you should probably completely ignore this approach as it alters the main WP loop – the one in charge of displaying whatever the “current page” is supposed to be based on the URL).
What is the difference between
get_posts() actually uses a
WP_Query object under the hood. The unexpected thing it does, though, is set up the
WP_Query object with a bunch of defaults that alter
WP_Query‘s default behavior instead of using
Some notable arguments that get changed by default are described below.
suppress_filters is enabled in
If you use a plugin filtering the posts returned by WP using one of the filters below, the filtering is not going to work if you use
get_posts(). That is, unless you manually set
'suppress_filters' => false.
This is really important to take into account especially if you plan to restrict access to some posts for some users.
ignore_sticky_posts is enabled in
Sticky posts will not be included in the returned list of posts.
no_found_rows is disabled in
This is actually a pretty cool optimization. The only problem with this is that you don’t always want this to happen.
By default WordPress adds
SQL_CALC_FOUND_ROWS to your
SELECT SQL query. When WP asks the database for a list of posts, by default it will not only return the matching posts but it will also include information on how many matching posts in total there are in the database. This is crucial for pagination – knowing how many posts there are allows us to calculate how many post pages there are.
But what if we only want to display the 5 latest posts matching some criteria with no pagination? We don’t need the information on how many matching posts there are in total. This is when we can remove
SQL_CALC_FOUND_ROWS from the query, improving our site’s performance.
Leave a Reply