6 Responses

  1. Konstantin Kovshenin
    Konstantin Kovshenin July 19, 2012 at 17:01 | | Reply

    query_posts() might be used in one and only case if you need to modify main query of page (for which there are better and more reliable methods to accomplish, over simplistic approach of this function).

    Wrong 🙂 All three methods (query_posts, new WP_Query and get_posts) will issue a new query, so all three must be used in one and only one case: a secondary loop. If you want to modify the primary (main) loop you should do that before the query runs and the two best places for that are: the pre_get_posts action, and the request filter.

    It sets a lot of global variables and will lead to obscure and horrible bugs if used in any other place and for any other purpose

    It really just sets only one — the $wp_query global 🙂 It actually destroys it first though! It destroys the link to $wp_the_query and assigns a new WP_Query to $wp_query, and wp_reset_query restores back the link to $wp_the_query. It’s quite simple and straightforward if you read the wp-includes/query.php file.

    Actually I’m tired of repeating this over and over again, so why don’t we all just go and watch Nacin’s talk about WP_Query which has been hanging on the homepage of WordPress.tv for ages now:

    http://wordpress.tv/2012/06/15/andrew-nacin-wp_query/

    And if you ask me, query_posts should be deprecated. Without warning.

    ~ K

    1. Piet
      Piet July 20, 2012 at 11:43 | | Reply

      +1 Konstantin!

      better to create one’s own content than republishing others content without really understanding it…

  2. Rarst
    Rarst July 20, 2012 at 15:01 | | Reply

    That answer is originally two years old and nowadays people are eager to poke holes in it, so I did new editorial pass there. 🙂 Also why push again query_posts() is now thankfully much more prominent, newer techniques does not really invalidate basic loop mechanics in template stage (which is all my answer and diagram were essentially about).

    Also if you are copying content in full from WPSE answer please make it clear under terms of the license (CC-BY-SA) and consider following attribution requirements http://blog.stackoverflow.com/2009/06/attribution-required/

  3. Dhimas Kirana
    Dhimas Kirana April 20, 2014 at 19:49 | | Reply

    Hi, Jean Galea

    Nice post! Now, I know the difference. For me, it turned out better using WP_Query. Thanks 😀

Leave a Reply