Is it better practice to use query_posts, WP_Query, or get_posts to create various custom loops within a Page?

Hi @janoChen:

If you have a choice, go with WP_Query. Both of the other functions (query_posts() and get_posts()) call WP_Query indirectly.

The former is designed to allow you to modify the main query after the standard query has already been run, for example when you want a second loop. But query_posts() affects global variables and can have side-effects. If possible use WP_Query instead and your code will be more robust.

As for get_posts(), it’s just a wrapper around WP_Query with some potentially unexpected defaults so you might as well call WP_Query directly and avoid those issues.

Leave a Comment