“Function Reference/WP Query”的版本间的差异
小 （→Properties： formatting error）
2009年3月14日 (六) 09:23的版本
Role of WP_Query
ttWP_Query/tt is a class defined in ttwp-includes/query.php/tt that deals with the intricacies of a request to a WordPress blog. The ttwp-blog-header.php/tt (or the WP class in Version 2.0) gives the tt$wp_query/tt object information defining the current request, and then tt$wp_query/tt determines what type of query it's dealing with (category archive? dated archive? feed? search?), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.
Methods and Properties
This is the formal documentation of ttWP_Query/tt. You shouldn't alter the properties directly, but instead use the methods to interact with them. Also see Interacting with WP_Query for some useful functions that avoid the need to mess around with class internals and global variables.
dl dttt$query/ttddHolds the query string that was passed to the $wp_query object by wp-blog-header.php (or the WP class in Version 2.0). For information on the construction of this query string (in WP1.5 with wp-blog-header.php - the following link is fairly outdated now), see WordPress Code Flow. dttt$query_vars/ttddAn associative array containing the dissected tt$query/tt: an array of the query variables and their respective values. dttt$queried_object/ttddApplicable if the request is a category, author, permalink or Page. Holds information on the requested category, author, post or Page. dttt$queried_object_id/ttddSimply holds the ID of the above property. dttt$posts/ttddGets filled with the requested posts from the database. dttt$post_count/ttddThe number of posts being displayed. dttt$current_post/ttdd(available during The Loop) Index of the post currently being displayed. dttt$post/ttdd(available during The Loop) The post currently being displayed. dttt$is_single/tt, tt$is_page/tt, tt$is_archive/tt, tt$is_preview/tt, tt$is_date/tt, tt$is_year/tt, tt$is_month/tt, tt$is_time/tt, tt$is_author/tt, tt$is_category/tt, tt$is_tag/tt, tt$is_tax/tt, tt$is_search/tt, tt$is_feed/tt, tt$is_comment_feed/tt, tt$is_trackback/tt, tt$is_home/tt, tt$is_404/tt, tt$is_comments_popup/tt, tt$is_admin/tt, tt$is_attachment/tt, tt$is_singular/tt, tt$is_robots/tt, tt$is_posts_page/tt, tt$is_paged/ttddBooleans dictating what type of request this is. For example, the first three represent 'is it a permalink?', 'is it a Page?', 'is it any type of archive page?', respectively. /dl
(An ampersand () before a method name indicates it returns by reference.)
- Initialise the object, set all properties to null, zero or false.
- Takes a query string defining the request, parses it and populates all properties apart from tt$posts/tt, tt$post_count/tt, tt$post/tt and tt$current_post/tt.
- Reparse the old query string.
- Get a named query variable.
- ttset($query_var, $value)/tt
- Set a named query variable to a specific value.
- Fetch and return the requested posts from the database. Also populate tt$posts/tt and tt$post_count/tt.
- (to be used when in The Loop) Advance onto the next post in tt$posts/tt. Increment tt$current_post/tt and set tt$post/tt.
- (to be used when in The Loop) Advance onto the next post, and set the global tt$post/tt variable.
- (to be used when in The Loop, or just before The Loop) Determine if we have posts remaining to be displayed.
- Reset tt$current_post/tt and tt$post/tt.
- Call ttparse_query()/tt and ttget_posts()/tt. Return the results of ttget_posts()/tt.
- Set tt$queried_object/tt if it's not already set and return it.
- Set tt$queried_object_id/tt if it's not already set and return it.
- ttWP_Query($query = nowiki/nowiki)/tt (constructor)
- If you provide a query string, call ttquery()/tt with it.
Interacting with WP_Query
Most of the time you can find the information you want without actually dealing with the class internals and globals variables. There are a whole bunch of functions that you can call from anywhere that will enable you to get the information you need.
There are two main scenarios you might want to use ttWP_Query/tt in. The first is to find out what type of request WordPress is currently dealing with. The tt$is_*/tt properties are designed to hold this information: use the Conditional Tags to interact here. This is the more common scenario to plugin writers (the second normally applies to theme writers).
The second is during The Loop. ttWP_Query/tt provides numerous functions for common tasks within The Loop. To begin with, tthave_posts()/tt, which calls tt$wp_query-have_posts()/tt, is called to see if there are any posts to show. If there are, a ttwhile/tt loop is begun, using tthave_posts()/tt as the condition. This will iterate around as long as there are posts to show. In each iteration, ttthe_post()/tt, which calls tt$wp_query-the_post()/tt is called, setting up internal variables within tt$wp_query/tt and the global tt$post/tt variable (which the Template Tags rely on), as above. These are the functions you should use when writing a theme file that needs a loop. See also The Loop and The Loop in Action for more information.