全局WP_Query中$ query和$ query_vars之间的区别是什么

时间:2014-09-04 10:32:13

标签: php wordpress

来自docs

$query
Holds the query string that was passed to the $wp_query object by WP class.

$query_vars
An associative array containing the dissected $query: an array of the query variables and their respective values.

但是当我做这样的事情时:

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 10,
    'post_status' => 'publish'
);
$author_query = new WP_Query( $args );

然后我可以从$query_vars看到所有这些参数转到print_r($wp_query),那么$query的目的是什么,我该如何调整此属性的值。

我很好奇,因为当我转到作者模板时,此页面中的query属性包含[author_name] => admin之类的内容?

1 个答案:

答案 0 :(得分:1)

$query是一个数组,其中包含在调用WP_Query时传递的值,即自定义值。

$query_vars是一个包含WP_Query支持的所有参数的数组,包括调用WP_Query时的参数usen以及其他具有默认值的参数。

编辑:您看到author_name集的原因是Wordpress从URL获取值,但您可以使用自定义查询或某些过滤器传递它。当Wordpress在网址中检测到该参数时,会自动匹配正确的模板,您可以在template hierarchy

中查看其工作原理
相关问题