WP_Query Query_Posts And Get_Posts

Headching by just looking at the title?

When I first started programming in wordpress, I was really confused by the query tools that are at our disposal, wondering that if there is any difference between them and what is the difference.

This wordpress document has rightly pointed out that:

query_posts is in charge of your main loop called The Loop! If you want to create separate Loops outside of the main one, you should use get_posts() instead.

How to understand this?

From my experience of working with open source projects, there is one important place for you to get a handle of the system, that is source code, I usually find that the source code is usually the best document that provides the most direct information and leads you directly to the answer of a question. The only precondition is to have some knowledge of that programming language.

Back to the question.

WP_Query is a class and query_posts is a function that creates a new WP_Query object and assign it to global wp_query variable, and both of them are defined in query.php

You are confused, aren’t you? have a look of query.php, it defines a lot of global variables and functions like have_posts, the_post etc, why I call them global? as they are all pointing to a global object wp_query which is an instance of WP_Query and created by query_posts function.

In plain language: query_posts replaces wp_query(wp_query is the global object, an instance of class WP_Query), therefore determines what posts are accessible to readers, and normally it is not a good idea to use this anywhere in your little widgets, you can use this unless you know what you are doing, for example you want to only allow some posts not all posts to be accessible by readers, this is right handle you can use.

While underneath of query_posts it is using WP_Query, you can use WP_Query in the following way without worrying about messing up

  $newquery = new WP_Query('aquerystring');
  while ($newquery->have_posts())
  {
    $apost = $newquery->next_post();
  }

So you can see here, you can do everything you like using WP_Query, but to remember to add the object reference(here is $newquery), if the object reference is missing, then it is referencing to wp_query.

As an example, while(have_post()) , you are referencing the global wp_query, can you see the difference now from the way it is used in the above code snippet?

A note about the_post(), if you do $newquery->the_post(), you need to go to global $post to get it, at the same time your $newquery post is overriding global $post even you are doing $newquery->the_post(). so be careful.

get_posts is implemented in post.php, it creates a new WP_Query, and through this instance to execute the query, in the end an array of posts being returned, there is no opportunity to override anything in the global area while it has your job done.

Tags:

This entry was posted on Tuesday, November 2nd, 2010 at 1:50 am and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*