Difference Between WP_Query vs query_posts() vs get_posts()

All 3 of these functions supposedly do the same thing but there are differences between them and we are going to explain by example so you could understand how to use each of them and which is the best one.

Summary

in this article we will discuss which functions is the best for getting/retrieving posts from database in wordpress and what is the difference between them and which one is the safest and easiest to use. we begin by introducing query_posts() then it’s cons and they we will talk about get_posts() and at the end we will talk about WP_Query() class.

in brief:

  • What is query_posts and how to use it
    • Example Code for query_posts
  • What is get_posts and how to use it
    • Example Code for get_posts
  • What is WP_Query and example to use it
    • Example Code for WP_Query

Example Use of query_posts() function

this function is very simple to use but at the same time very problematic, it works by modifying the main query of current page and replacing it with a new one that you define here, it changes the $wp_query variable that wordpress uses. it is not efficient and it repeats the query executions, and eventually it will fail on some cases(specially when you have pagination on the page) because some themes and plugins might depend on this function and when you change it, those plugin/theme functionalities won’t function as they should.

in most cases you should avoid using these and try to hook into pre_get_posts instead. we don’t recommend using this function to get the posts but here is an example of using query_posts function:


another way is :

Example use of get_posts() function

this function is very similar to query_posts() function and you can use it the same way, but the difference is that it will return an array of posts and it won’t change the global variables like query_posts() function, so you can use it safely and with out being worried that it would be problematic.

you can also use this function to create multiple loops (example: list 10 articles from each category on the same page).

here is an example use of get_posts() function :

Example use of WP_Query() function in wordpress themes and plugins

both the get_posts() and query_posts() functions use WP_Query class in behind the scenes, you can work with it directly as well and create an object of your own. it’s a bit complicated to use but it has less restrictions and like get_posts() function it’s very safe to use.

here is an example of how to use it :


 

Conclusion

so we discussed about the three function in wordpress that we can use to retrieve a list of posts and concluded that the best functions and the safes ones are get_posts() function and WP_Query() function. you can use both of them to list articles from wordpress, our suggestion is if you are a newbie to wordpress use get_posts() because it’s easier to use, you can also use WP_Query but it’s a bit harder than get_posts() function.  from the development point of view, both these functions do the same things, get_posts() is a wrapper function for WP_Query, so behind the scenes more or less they are both the same.

please let us know in the comments if this was helpful to you or if you have any tips or questions regarding this article.

 

 


Rating:
1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 5.00 out of 5)
Loading...

Leave a Reply

Be the First to Comment!

avatar
wpDiscuz

Pin It on Pinterest

Share This
FreeAmazing WordPressSubscription

FreeAmazing WordPressSubscription

Join our mailing list to receive the latest news and updates about wordpress themes and plugins. we don't spam, we only send unique weekly emails about wordpress.

You have Successfully Subscribed!