WP_Queryfor secondary requests and queries.
wp_reset_queryat all costs.
suppress_filtersand use the value
false, so that caches get used. If you do not set this, there will be a performance penalty. This is not needed for
WP_Query( best )
get_posts( okay )
query_posts( avoid at all costs )
WP_Queryobject represents a query, e.g. the main query, and has helpful methods such as:
the_post();found in most themes are wrappers around the main query object:
get_postsis similar to
WP_Query, and takes the same arguments, but it returns an array containing the requested posts in full. You shouldn't use
get_postsif you're intending to create a post loop.
get_postsis conceptually simpler than
WP_Queryfor novice programmers to understand, it does have a downside.
get_postsdoesn't make extensive use of the object cache in the way that
WP_Querydoes, and may not be as performant.
false. By default,
get_postsignores cache, and will run slower.
query_postsis an overly simplistic and problematic way to modify the main query of a page by replacing it with new instance of the query.
pre_get_postshook, for this purpose. Do not use
get_posts, you may set the current post object, using
setup_postdata. If you do, you need to clean up after yourself when you finish your while loop. Do this by calling
wp_reset_postdataafter the if statement. This is incorrect, as the post hasn't changed if the if statement is false, leading to potentially unexpected behavior. Always call the function before the closing brace, not after, e.g.
query_posts, you will need to restore the main query after you've done your work. Failure to do so can lead to a large number of issues and unexpected behavior. You can do this with
wp_reset_query. Always do this after calling
query_posts, and only do it when necessary. This means you should never need to use this function.
functions.php, or in a plugin.