Make Sense of WP Query Functions

Make Sense of WP Query Functions

Make Sense of WP Query Functions

by admin with 0 Reply in

Thanks Rarst and Stackexchange, one image tell us how to correctly use query_posts() , get_posts() and WP_Query.

  • query_posts() is overly simplistic and problematic way to to modify main query of page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with pagination). Any modern WP code should use more reliable methods, like making use ofpre_get_postshook, for this purpose. TL;DR don’t use query_posts() ever;
  • get_posts() is very similar in usage and accepts same arguments (with some nuances, like different defaults), but returns array of posts, doesn’t modify global variables and is safe to use anywhere;
  • WP_Query class powers both behind the scenes, but you can also create and work with own object of it. Bit more complex, less restrictions, also safe to use anywhere.


Tags:

Get Access To Our Entire Collection Of 53 Beautiful Themes For The Price Of One

We offer a 30 Day Money Back Guarantee, so joining is risk-free!

OK, Join Now!