http://codex.wordpress.org/Function_Reference/WP_Query
Contents
[hide]
Role of WP_Query
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query 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 WP_Query. 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.
Properties
- $query
- Holds 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.
- $query_vars
- An associative array containing the dissected $query: an array of the query variables and their respective values.
- $queried_object
- Applicable if the request is a category, author, permalink or Page. Holds information on the requested category, author, post or Page.
- $queried_object_id
- Simply holds the ID of the above property.
- $posts
- Gets filled with the requested posts from the database.
- $post_count
- The number of posts being displayed.
- $current_post
- (available during The Loop) Index of the post currently being displayed.
- $post
- (available during The Loop) The post currently being displayed.
- $is_single, $is_page, $is_archive, $is_preview, $is_date, $is_year, $is_month, $is_time, $is_author, $is_category, $is_tag, $is_tax, $is_search, $is_feed, $is_comment_feed, $is_trackback, $is_home, $is_404, $is_comments_popup, $is_admin, $is_attachment, $is_singular, $is_robots, $is_posts_page, $is_paged
- Booleans 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.
Methods
(An ampersand (&) before a method name indicates it returns by reference.)
- init()
- Initialise the object, set all properties to null, zero or false.
- parse_query($query)
- Takes a query string defining the request, parses it and populates all properties apart from $posts, $post_count, $post and $current_post.
- parse_query_vars()
- Reparse the old query string.
- get($query_var)
- Get a named query variable.
- set($query_var, $value)
- Set a named query variable to a specific value.
- &get_posts()
- Fetch and return the requested posts from the database. Also populate $posts and $post_count.
- next_post()
- (to be used when in The Loop) Advance onto the next post in $posts. Increment $current_post and set $post.
- the_post()
- (to be used when in The Loop) Advance onto the next post, and set the global $post variable.
- have_posts()
- (to be used when in The Loop, or just before The Loop) Determine if we have posts remaining to be displayed.
- rewind_posts()
- Reset $current_post and $post.
- &query($query)
- Call parse_query() and get_posts(). Return the results of get_posts().
- get_queried_object()
- Set $queried_object if it’s not already set and return it.
- get_queried_object_id()
- Set $queried_object_id if it’s not already set and return it.
- WP_Query($query = '') (constructor)
- If you provide a query string, call query() 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 WP_Query in. The first is to find out what type of request WordPress is currently dealing with. The $is_* 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. WP_Query provides numerous functions for common tasks within The Loop. To begin with, have_posts(), which calls $wp_query->have_posts(), is called to see if there are any posts to show. If there are, a while loop is begun, using have_posts() as the condition. This will iterate around as long as there are posts to show. In each iteration, the_post(), which calls $wp_query->the_post() is called, setting up internal variables within $wp_query and the global $post 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.
Retrieved from "http://codex.wordpress.org/Function_Reference/WP_Query"
函数参考/可湿性粉剂查询
Contents目录
[hide]
- 1 Role of WP_Query 1 作用WP_Query
- 2 Methods and Properties 2 方法和属性
- 3 Interacting with WP_Query 3 相互作用WP_Query
Role of WP_Query 作用WP_Query
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. WP_Query是一类定义wp-includes/query.php ,处理的复杂性要求一个WordPress的博客。 The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it’s dealing with (category archive? dated archive? feed? search?), and fetches the requested posts.在涡喷博客- header.php (或可湿性粉剂类2.0版)提供了$ wp_query对象的信息确定当前请求,然后$ wp_query确定哪些类型的查询的处理(分类存档?月存档?供稿?搜索? ) ,并获取所要求的职位。 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 WP_Query .这是正式文件WP_Query 。 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.还看到相互作用WP_Query的一些有用的功能,避免需要更动阶级内部和全局变量。
Properties 属性
- $query $查询
- Holds the query string that was passed to the $wp_query object by wp-blog-header.php (or the WP class in Version 2.0).认为这是查询字符串传递到$ wp_query对象的涡喷博客- header.php (或可湿性粉剂类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 .如需建设这个查询字符串(在WP1.5与涡喷博客- header.php -下面的链接是相当过时的现在) ,见WordPress的码流 。
- $query_vars $ query_vars
- An associative array containing the dissected $query : an array of the query variables and their respective values.关联数组包含$解剖查询 :一个数组的查询变量和它们各自的价值观念。
- $queried_object $ queried_object
- Applicable if the request is a category, author, permalink or Page.如果适用的要求是一个类别,作者,永久或页面。 Holds information on the requested category, author, post or Page.拥有所要求的信息类别,作者,张贴或网页。
- $queried_object_id $ queried_object_id
- Simply holds the ID of the above property.只要持有身份证上述财产。
- $posts $员额
- Gets filled with the requested posts from the database.得到填补职位的要求从数据库。
- $post_count $ post_count
- The number of posts being displayed.员额数目显示。
- $current_post $ current_post
- (available during The Loop ) Index of the post currently being displayed. (可在环 )指数的职位目前正在显示出来。
- $post $后
- (available during The Loop ) The post currently being displayed. (可在环 )后正在显示出来。
- $is_single , $is_page , $is_archive , $is_preview , $is_date , $is_year , $is_month , $is_time , $is_author , $is_category , $is_tag , $is_tax , $is_search , $is_feed , $is_comment_feed , $is_trackback , $is_home , $is_404 , $is_comments_popup , $is_admin , $is_attachment , $is_singular , $is_robots , $is_posts_page , $is_paged $ is_single , $ is_page , $ is_archive , $ is_preview , $ is_date , $ is_year , $ is_month , $ is_time , $ is_author , $ is_category , $ is_tag , $ is_tax , $ is_search , $ is_feed , $ is_comment_feed , $ is_trackback , $ is_home , $ is_404 , $ is_comments_popup , $ is_admin , $ is_attachment , $ is_singular , $ is_robots , $ is_posts_page , $ is_paged
- Booleans 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.例如,第一三个代表’是一个永久性? ‘ , ‘是网页? ‘ , ‘是任何类型的档案网页? ‘分别。
Methods 方法
(An ampersand (&) before a method name indicates it returns by reference .) (一个符号( & )在方法名称表明它的回报参考 。 )
- init() 的init ( )
- Initialise the object, set all properties to null, zero or false.初始化的对象,所有属性设置为null ,零或FALSE 。
- parse_query($query) parse_query (查询)
- Takes a query string defining the request, parses it and populates all properties apart from $posts , $post_count , $post and $current_post .需要查询字符串界定的要求,剖析,并填充所有属性除了$员额 , $ post_count , $后美元current_post 。
- parse_query_vars() parse_query_vars ( )
- Reparse the old query string.重旧的查询字符串。
- get($query_var) 获得( $ query_var )
- Get a named query variable.获取一个命名查询变量。
- set($query_var, $value) 一套( $ query_var , $值)
- Set a named query variable to a specific value.设置一个命名查询变量一个特定的值。
- &get_posts() & get_posts ( )
- Fetch and return the requested posts from the database.提取和归还所要求的员额从数据库。 Also populate $posts and $post_count .还填充$员额和$ post_count 。
- next_post() next_post ( )
- (to be used when in The Loop ) Advance onto the next post in $posts . (时使用的循环 )提前到明年员额$员额 。 Increment $current_post and set $post .增量$ $ current_post并设置后 。
- the_post() the_post ( )
- (to be used when in The Loop ) Advance onto the next post, and set the global $post variable. (时使用的循环 )推进到下一个职位,并设定全球$后变量。
- have_posts() have_posts ( )
- (to be used when in The Loop , or just before The Loop) Determine if we have posts remaining to be displayed. (时使用的循环 ,或者只是前环路)确定,如果我们有个剩余显示。
- rewind_posts() rewind_posts ( )
- Reset $current_post and $post .重置$ current_post后美元。
- &query($query) &查询(查询)
- Call parse_query() and get_posts() .呼叫parse_query ( )和get_posts ( ) 。 Return the results of get_posts() .返回的结果get_posts ( ) 。
- get_queried_object() get_queried_object ( )
- Set $queried_object if it’s not already set and return it.集$ queried_object如果尚未设置和恢复。
- get_queried_object_id() get_queried_object_id ( )
- Set $queried_object_id if it’s not already set and return it.集$ queried_object_id如果尚未设置和恢复。
- WP_Query($query = '') (constructor) WP_Query ( $查询= '' ) (构造)
- If you provide a query string, call query() with it.如果您提供了一个查询字符串,请查询( )的。
Interacting with WP_Query 相互作用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 WP_Query in.主要有两种情况下您可能需要使用WP_Query英寸 The first is to find out what type of request WordPress is currently dealing with.首先是找出什么样的要求,目前WordPress的处理。 The $is_* properties are designed to hold this information: use the Conditional Tags to interact here.美元is_ *性能的目的是保持这样的信息:使用条件标记的互动这里。 This is the more common scenario to plugin writers (the second normally applies to theme writers).这是较常见的情况下,以插件作家(第二通常适用于主题作家) 。
The second is during The Loop . WP_Query provides numerous functions for common tasks within The Loop.第二个是在环 。 WP_Query提供了许多功能的共同任务的循环。 To begin with, have_posts() , which calls $wp_query->have_posts() , is called to see if there are any posts to show.首先, have_posts ( ) ,其中要求$ wp_query , “ have_posts ( ) ,是所谓的联系,看看是否有任何职位,以查看。 If there are, a while loop is begun, using have_posts() as the condition.如果有一个while循环开始,利用have_posts ( )为条件。 This will iterate around as long as there are posts to show.这将迭代周围只要有职位,以查看。 In each iteration, the_post() , which calls $wp_query->the_post() is called, setting up internal variables within $wp_query and the global $post variable (which the Template Tags rely on), as above.在每一次迭代, the_post ( ) ,其中要求$ wp_query , “ the_post ( )被调用,设立内部变量在$ wp_query和全球$后变量(其中模板标记依靠) ,如上。 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.又见环和环在行动中获得更多信息。
Retrieved from " http://codex.wordpress.org/Function_Reference/WP_Query "取自“ http://codex.wordpress.org/Function_Reference/WP_Query ”



◇评论◇