WordPress:Query Overview

来自站长百科
跳转至: 导航、​ 搜索

This article is an overview for developers of the process WordPress uses to build your blog pages, and how plugins can modify that process. It is aimed at developers of plugins that will do advanced queries and permalinks, and also at developers who want to understand WordPress better, in order to add new features to the core of WordPress or fix WordPress bugs.

这篇文章是写给开发人员的概述,关于创建博客网页的过程,以及插件怎样更改这个过程。这篇文章是写给插件开发人员的,他们做高级查询和permalinks,同时也是写给想要更深入地了解WordPress的开发人员的,想要向WordPress核心添加新的功能或者解决WordPress程序错误的开发人员。

For more details, you'll need to read the WordPress core PHP files and functions mentioned.

更多的信息,你需要阅读WordPress核心PHP文件和提及的函数。

So, here are the steps WordPress uses to decide what posts or pages to display on a page, and display them:

WordPress执行下面的步骤,决定在一个网页上显示那篇文章或者那些网页,并且显示这些文章或者网页。

  1. When a visitor first clicks on or types a URL for a page that is part of your blog, WordPress starts by running a few core files (wp-config.php, wp-settings.php, etc.) If you are interested in the specifics of the file loading order, start at index.php and follow the chain of files as each PHP file includes/requires additional PHP files.
  1. 当访客首次点击你的博客网页或者输入你的博客中一个网页的URL时,WordPress就开始运行一些核心文件(wp-config.php, wp-settings.php, 等等。)如果你对文件载入顺序的详细信息,感兴趣,你可以从index.php开始,紧跟着一系列文件,因为每个PHP文件包含/需要额外的PHP文件。
  1. WordPress loads and initializes any plugins you have activated (calls the plugin init actions).
  1. 任何你激活的插件,WordPress都会载入并且对其初始化(称作插件 init活动)。
  1. WordPress loads the "text domain" for internationalization, and the functions.php file from the currently active theme.
  1. 为了国际化的需求,WordPress载入了"text domain",同时从当前运行的主题中,载入functions.php文件。
  1. WordPress runs the wp() function (in wp-includes/functions.php), which calls $wp->main() ($wp is an object of class WP, which is defined in wp-includes/classes.php). This tells WordPress to:
  1. WordPress运行wp()函数(在wp-includes/functions.php中),调用$wp->main() ($wp is an object of class WP, 定义在wp-includes/classes.php中)。这命令WordPress:
    1. Parse the URL into a query specification using WP->parse_request() -- more on that below.
    1. 使用WP->parse_request()将URL解析为查询specification—下面有更多的内容。
    1. Set all the is_ variables that are used by WordPress:Conditional Tags using $wp_query->parse_query() ($wp_query is an object of class WP_Query, which is defined in wp-includes/query.php). Note that in spite of this function's name, in this case WP_Query->parse_query doesn't actually do any parsing for us, since that is done before-hand by WP->parse_request().
    1. 使用$wp_query->parse_query()($wp_query is an object of class WP_Query, 定义在wp-includes/query.php中),设置条件式标签使用的所有参数。注意尽管函数的名称是那样的,在这个例子中,WP_Query->parse_query不为我们做任何解析,因为解析是WP->parse_request()预先执行的。
## Convert the query specification into a MySQL database query, and run the database query to get the list of posts, in function WP_Query->get_posts(). Save the posts in the $wp_query object to be used in the WordPress Loop.
    1. 将查询specification转换为MySQL数据库查询,运行数据库查询得到函数WP_Query->get_posts()中文章的列表。将文章保存到用在WordPress Loop中的$wp_query object。
    1. Handle 404 errors.
    1. 处理404错误。
    1. Send the blog's HTTP headers.
    1. 发送博客的HTTP标头。
    1. Set up some variables for the WordPress Loop.
    1. 为WordPress Loop设置一些变数。
  1. WordPress loads your template, figures out which template file to use according to the WordPress:Template Hierarchy, and runs that file (basically doing whatever your template file says to do). Or, WordPress could run one of the feed files (such as wp-rss2.php) instead.
  1. WordPress载入你的模板,根据模板层级决定使用哪个模板文件,并且运行那个文件(基本上是执行模板文件的命令)。或者WordPress可以运行其中的一个feed文件(例如wp-rss2.php)。
  1. Generally, the template or feed file runs the WordPress Loop to print blog posts or a static page.
  1. 一般来说,模板或者feed文件运行WordPress Loop打印博客文章或者静态网页。
  1. The template or feed file will also likely print out permalinks to some archives, categories, or posts using built-in WordPress functions.
  1. 模板或者feed文件也有可能使用内置WordPress函数,为一些归档,类别,后者文章打印permalinks。

More on WP->parse_request()[ ]

更多关于 WP->parse_request()的内容[ ]

As mentioned above, WP->parse_request() (part of class WP in wp-includes/classes.php) parses a URL into a query specification. Here is a summary of the steps it uses to do this:

如上所述,WP->parse_request() (class WP in wp-includes/classes.php的一部分)将URL解析为查询specification。下面是这个步骤的总结:

  1. Strips the GET variable section out of the URL (i.e. anything after a "?" in the URL). Also strips out the blog's home URL.
  1. 去除URL中的得到变数部分(例如,URL中"?"后的任何内容)。同时去除博客主页URL。
  1. Obtains the rewrite rules that are currently in effect, by calling $wp_rewrite->wp_rewrite_rules() ($wp_rewrite is an object of class WP_Rewrite, which is defined in wp-includes/rewrite.php). The rewrite rules are basically a set of pattern matching rules for WordPress permalinks, with a specification of what to do if the pattern matches. For instance, by default there is a rule that would match a stripped permalink like category/abc, and its specification says that it means the "abc" category was requested. There is also a rewrite rule for the home page (nothing after the blog URL).
  1. 通过调用$wp_rewrite->wp_rewrite_rules() ,得到当前有效的rewrite rules ($wp_rewrite是an object of class WP_Rewrite, 定义在wp-includes/rewrite.php中)。重写规则基本上是一组WordPress permalinks的模式匹配规则,规定了模式匹配时,应该做什么。例如,默认情况下,有一个规则会匹配stripped permalink像 category/abc, 而且规定的意思是需要"abc" 类别。主页也有一个重写规则(博客URL后什么内容也没有)。
  1. Goes through the rewrite rules in order, until it finds a match between a rewrite rule and the permalink. If nothing is found, it's a 404 error. If a match is found, WordPress extracts the information according to the rule specification.
  1. 按顺序查看重写规则,知道找到重写规则和permalink之间的一个匹配。如果什么匹配也没有发现,返回404错误。如果找到了匹配,WordPress根据规则,提取信息。
  1. Obtains the list of query variables that is currently in effect. For each query variable, WordPress checks to see if it has been set by permalink parsing, POST submission, or GET submission, and if so, WordPress saves the variable's value into the query specification array ($wp->query_vars, part of class WP in wp-includes/classes.php).
  1. 得到当前有效的一列查询变数。对于每个查询变数,WordPress检查看这个变数是否由permalink 解析,文章递交,或者得到递交设置了,如果已经设置了,WordPress将变数的值保存到query specification数组($wp->query_vars, class WP in wp-includes/classes.php的一部分)。

What Plugins can Modify[ ]

插件可以更改什么[ ]

Here is an overview of the things a plugin can do to modify the default query and permalink behavior described above. Many of these modifications are described (with examples) in the article WordPress:Custom Queries.

下面是一个概述,关于插件可以怎样做,更改默认查询和上述的permalink。许多这样的更改在自定义查询中有描述(带有例子)。

  • Add, modify, or remove rewrite rules, to affect how permalinks are parsed. This is generally not done with filters and actions, but instead by calling functions in wp-includes/rewrite.php, such as add_rewrite_rule, add_rewrite_endpoint, etc. This can be a bit tricky, because WP_Rewrite->wp_rewrite_rules() usually just gets the previously-saved set of rewrite rules (they are saved in the WordPress database as option "rewrite_rules"). So if you want to modify rewrite rules, you will need to call $wp_rewrite->flush_rules() to force them to recalculate. You'll need to do this in your plugin's init action, so that it happens early enough in the process.
  • 添加,更改,或者移除重写规则,会影响permalinks的解析方式。这些通过不是过滤器或者actions完成的,而是通过调用wp-includes/rewrite.php中的函数,例如add_rewrite_rule, add_rewrite_endpoint,等等。这样做有些微妙,因为WP_Rewrite->wp_rewrite_rules()通常只得到先前保存的改写规则(这些规则保存在 WordPress 数据库作为选项"rewrite_rules")。因此,如果你想要更改改写规则,你需要在你的插件的init操作中更改,因此更改过程很早就发生了。
  • Add or remove query variables, to affect which variables are saved in the query specification from POST, GET, and permalink requests (query_vars filter).
  • 添加或者移除查询变数,影响哪个变数保存在来自文章,GET和permalink请求(query_vars filter)的查询specification。
  • Modify the query specification, after variable values are saved (request filter or parse_request action; if you want to use conditional tag tests, use the parse_query or pre_get_posts action, as these run after the is_ variables are set).
  • 变数值保存后,更改query specification(request 过滤器或者parse_request action; 如果你想使用条件式标签测试,请使用 parse_query 或者 pre_get_posts action, 因为is_ 变数设置好之后,这些才会运行)。
  • Modify the MySQL database query, after it is created from the query specification (posts_where, posts_join, posts_groupby, posts_orderby, posts_distinct, posts_fields, post_limits, posts_where_paged, posts_join_paged, and posts_request filters).
  • MySQL数据库query从query specification中创建好之后,更改MySQL数据库query(posts_where, posts_join, posts_groupby, posts_orderby, posts_distinct, posts_fields, post_limits, posts_where_paged, posts_join_paged, 和posts_request 过滤器)。
  • Modify the results of the database query (the_posts filter).
  • 更改数据库query的结果(the_posts 过滤器)。
  • Override the default template file choice (template_redirect action).
  • 废除默认模板文件选择(template_redirect action)。