Why you should avoid using query_posts()

Posted in Articles

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

The WordPress function query_posts() will alter the main loop which means that post-related global variables and template tags will be altered.  This should be avoided as it make unintended bugs that are difficult to locate.

According to the WordPress codex

“For general post queries, use WP_Query or get_posts. It is strongly recommended that you use the pre_get_posts filter instead, and alter the main query by checking is_main_query”

But if for some reason, you must use query_posts(), then you should call wp_reset_query() afterwards.