WordPress : WP_Query vs query_posts() vs get_posts() [Differences |Comparison]


WP_Query vs query_posts() vs get_posts()

  • query_posts() is overly simplistic and problematic way to modify main query of a 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 posts 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.

 

Image

Source 

For WordPress News Website in local languages, contact me.

For customized web applications, websites using PHP codeigniter, contact me.

Advertisements

One thought on “WordPress : WP_Query vs query_posts() vs get_posts() [Differences |Comparison]

  1. Pingback: Display the most Recent Post from a Specific Category | sandipshirsaleblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s