$ GLOBALS ['wp_the_query']与全局$ wp_query
-
-
我会说`global $ wp_query`只是在一行中回答您的问题!I would say `global $wp_query` just to answer your question in one line!
- 2
- 2016-03-14
- Sumit
-
有什么不同?What is the difference?
- 0
- 2016-03-14
- Nathan Powell
-
3 个回答
- 投票数
-
- 2016-03-14
您错过了一个
$GLOBALS['wp_query']
.为了所有目的,$GLOBALS['wp_query'] === $wp_query
.但是,$GLOBALS['wp_query']
可读性更好,应该代替$wp_query
(但仍会保留个人喜好)现在,在一个由独角兽统治世界的理想世界中,
的副本$GLOBALS['wp_the_query'] === $GLOBALS['wp_query'] === $wp_query
.默认情况下,这应该是正确的.如果我们看看这些全局变量的设置位置(wp-settings.php
),您将看到主查询对象存储在$GLOBALS['wp_the_query']
中,$GLOBALS['wp_query']
只是$GLOBALS['wp_the_query']
/** * WordPress Query object * @global WP_Query $wp_the_query * @since 2.0.0 */ $GLOBALS['wp_the_query'] = new WP_Query(); /** * Holds the reference to @see $wp_the_query * Use this global for WordPress queries * @global WP_Query $wp_query * @since 1.5.0 */ $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
之所以这样做,是因为WordPress看到了
query_posts
.function query_posts($query) { $GLOBALS['wp_query'] = new WP_Query(); return $GLOBALS['wp_query']->query($query); }
如您所见,
query_posts
将主查询对象设置为当前的自定义查询开始运行.这会破坏主查询对象的完整性,从而为您提供不正确的数据,因此依赖主查询对象的所有内容都会由于数据错误而损坏.解决此问题的一种方法是创建另一个全局变量来存储主要查询对象
$GLOBALS['wp_the_query']
,此版本在2.0.0版本中引入.这个新的全局保存主查询对象,而$GLOBALS['wp_query']
只是一个副本.通过wp_reset_query()
,我们现在可以将$GLOBALS['wp_query']
重置回原始主查询对象,以恢复其完整性.但这不是一个完美的世界,
query_posts
本身就是魔鬼.尽管有成千上万的警告,但人们仍然使用query_posts
.除了中断主查询之外,它还会重新运行主查询,这使得它与WP_Query
的常规自定义查询相比要慢得多.完成后,许多人也不会使用wp_reset_query()
重置query_posts
查询,这使query_posts
更加邪恶.因为我们对此无能为力,也无法停止使用
query_posts
的插件和主题,并且我们永远无法知道query_posts
查询是否已通过wp_reset_query()
,我们需要主查询对象的更可靠副本,我们知道它将为我们提供99.99999%可靠,正确的数据.这就是$GLOBALS['wp_the_query']
有用的地方,因为没有WordPress相关代码可以更改其值(除外,通过WP_Query
本身内部的过滤器和操作).快速证明,运行以下
var_dump( $GLOBALS['wp_the_query'] ); var_dump( $GLOBALS['wp_query'] ); query_posts( 's=crap' ); var_dump( $GLOBALS['wp_the_query'] ); var_dump( $GLOBALS['wp_query'] );
,然后检查结果.
$GLOBALS['wp_the_query']
不变,而$GLOBALS['wp_query']
不变.那么哪个更可靠?最后的注释,
$GLOBALS['wp_the_query']
不代替wp_reset_query()
.wp_reset_query()
应该始终与query_posts
一起使用,而query_posts
应该从不用过的.结论
如果您需要可靠的代码(几乎永远不会失败),请使用
$GLOBALS['wp_the_query']
,如果您相信并相信插件和主题代码并且认为没有人使用query_posts
或使用正确,请使用$GLOBALS['wp_query']
或$wp_query
重要编辑
现在在这个网站上回答问题已有两年了,我看到许多用户使用
$wp_query
作为局部变量,这反过来又破坏了主查询对象.这进一步增加了$wp_query
的漏洞.例如,有人对此
$wp_query = new WP_Query( $args );
从本质上讲与
query_posts
正在执行的操作完全相同You have missed one,
$GLOBALS['wp_query']
. For all purposes,$GLOBALS['wp_query'] === $wp_query
.$GLOBALS['wp_query']
is however better for readability and should be used instead of$wp_query
, BUT, that remains personal preferenceNow, in a perfect world where unicorns rule the world,
$GLOBALS['wp_the_query'] === $GLOBALS['wp_query'] === $wp_query
. By default, this should be true. If we look at where these globals are set (wp-settings.php
), you will see the main query object is stored in$GLOBALS['wp_the_query']
and$GLOBALS['wp_query']
is just a duplicate copy of$GLOBALS['wp_the_query']
/** * WordPress Query object * @global WP_Query $wp_the_query * @since 2.0.0 */ $GLOBALS['wp_the_query'] = new WP_Query(); /** * Holds the reference to @see $wp_the_query * Use this global for WordPress queries * @global WP_Query $wp_query * @since 1.5.0 */ $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
The reason for doing it this way, is because WordPress saw the arrival of
query_posts
in version 1.5.function query_posts($query) { $GLOBALS['wp_query'] = new WP_Query(); return $GLOBALS['wp_query']->query($query); }
As you can see,
query_posts
sets the main query object to the current custom query beign run. This breaks the integrity of the main query object, which gives you incorrect data, so anything that relies on the main query object is broken due to wrong data.A way to counter this was to create another global to store the main query object,
$GLOBALS['wp_the_query']
which was introduced in version 2.0.0. This new global hold the main query object and$GLOBALS['wp_query']
just a copy. Throughwp_reset_query()
, we could now reset$GLOBALS['wp_query']
back to the original main query object to restore its integrity.But this is not a perfect world, and
query_posts
are the devil himself. Although thousands of warnings, people still usequery_posts
. Apart from breaking the main query, it reruns the main query, making it much slower as a normal custom query withWP_Query
. Many people also do not reset thequery_posts
query withwp_reset_query()
when done, which makesquery_posts
even more evil.Because we cannot do anything about that, and cannot stop plugins and themes from using
query_posts
and we can never know if aquery_posts
query was reset withwp_reset_query()
, we need a more reliable copy of the main query object which we know will give us 99.99999% reliable, correct data. That is where$GLOBALS['wp_the_query']
is useful as no WordPress related code can change it's value (except through the filters and actions insideWP_Query
itself).Quick proof, run the following
var_dump( $GLOBALS['wp_the_query'] ); var_dump( $GLOBALS['wp_query'] ); query_posts( 's=crap' ); var_dump( $GLOBALS['wp_the_query'] ); var_dump( $GLOBALS['wp_query'] );
and check the results.
$GLOBALS['wp_the_query']
did not change, and$GLOBALS['wp_query']
has. So which is more reliable?Final note,
$GLOBALS['wp_the_query']
is NOT a replacement forwp_reset_query()
.wp_reset_query()
should always be used withquery_posts
, andquery_posts
should never be used.TO CONCLUDE
If you need reliable code which will almost always never fail, use
$GLOBALS['wp_the_query']
, if you trust and believe plugins and theme code and believe no one usesquery_posts
or is using it correctly, use$GLOBALS['wp_query']
or$wp_query
IMPORTANT EDIT
Being answering questions on this site now for a couple of years, I saw many users using
$wp_query
as a local variable, which in turn also breaks the main query object. This further increases the vulnerabilty of the$wp_query
.As example, some people to this
$wp_query = new WP_Query( $args );
which is in essence the exactly the same as what
query_posts
are doing-
[query_posts()](https://developer.wordpress.org/reference/functions/query_posts/)更改了全局$ wp_query.全局$ wp_the_query`拥有对** [主要查询]的引用(https://developer.wordpress.org/reference/classes/wp_query/is_main_query/)**[query_posts()](https://developer.wordpress.org/reference/functions/query_posts/) changes `global $wp_query`. `global $wp_the_query` holds the reference to **[the main query](https://developer.wordpress.org/reference/classes/wp_query/is_main_query/)**
- 1
- 2016-03-15
- Evan Mattson
-
我的评论并非旨在纠正,因此我对此表示歉意.我只是在总结一下(如果您愿意,可以使用TL; DR),同时指出我认为是$ wp_the_query的最重要方面之一,因为它与WP_Query ::is_main_query()方法有关,但并未提及:dMy comment wasn't intended as a correction, so my apologies if it did. I was merely summarizing (TL;DR if you will) while pointing out what I believe is one of the most significant aspects of `$wp_the_query` as it pertains to the `WP_Query::is_main_query()` method, which was not mentioned :D
- 0
- 2016-03-16
- Evan Mattson
-
@EvanMattson抱歉,我误解了您的第一条评论;-).是的,`is_main_query()`,是WP_Query ::is_main_query()的包装,WP_Query ::is_main_query()对照保存在`$ GLOBALS ['wp_the_query']'中的主查询对象检查当前查询对象.当您运行`pre_get_posts`操作并且只想定位主查询时,这是非常重要的;-)@EvanMattson Apologies, I misunderstood your first comment ;-). Yes, `is_main_query()`, which is a wrapper for `WP_Query::is_main_query()` which checks the current query object against the main query object saved in `$GLOBALS['wp_the_query']`. This is quite important when you run `pre_get_posts` actions and just want to target the main query ;-)
- 0
- 2016-03-16
- Pieter Goosen
-
做得不错!@EvanMattson应该是[编辑].Pretty well done answer! @EvanMattson That should have been an [edit].
- 0
- 2016-04-06
- kaiser
-
您可以在"重要编辑"部分中提及"is_main_query"功能吗?我今天使用的是`pre_get_posts`,因为我正在查看`$ wp_query`,所以发现使用该功能非常有用.Can you include mention of `is_main_query` function in the *IMPORTANT EDIT section? I was using `pre_get_posts` today and found it utterly useful to use that function since I was looking at `$wp_query`.
- 0
- 2017-03-18
- Nathan Powell
-
- 2016-03-14
global关键字将变量导入本地范围,而$ GLOBALS仅授予您访问变量的权限.
详细说明一下,如果您使用
global $wp_the_query;
您可以在本地范围内使用$wp_the_query
,而无需再次使用global.因此,基本上可以将global $wp_the_query
与$wp_the_query = $GLOBALS['wp_the_query']
编辑
我将wp_query误读为wp_the_query,因此我的答案不是问题的完整答案,但仍提供有关
global $variable
和$GLOBALS['variable']
The global keyword imports the variable into the local scope, while $GLOBALS just grants you access to the variable.
To elaborate, if you use
global $wp_the_query;
you can use$wp_the_query
inside the local scope without using the word global again. So basicallyglobal $wp_the_query
can be compared to$wp_the_query = $GLOBALS['wp_the_query']
EDIT
I misread wp_query for wp_the_query so my answer isn't a complete answer to the question but still provides general information about the difference between
global $variable
and$GLOBALS['variable']
-
请提交[edit],因为这确实不是原始问题的答案.仅供参考,`$ GLOBALS ['foo']`允许_overriding_或取消设置变量.因此,它比您在此处描述的还要多.Please, file an [edit] as this really is not an answer to the original question. Just FYI `$GLOBALS['foo']` allows _overriding_ or unsetting the variable as well. So it's a _bit_ more than what you describe here.
- 0
- 2016-04-06
- kaiser
-
- 2016-03-14
基本上一个是另一个的副本.查看
wp-settings.php
,第292-305行:$GLOBALS['wp_the_query'] = new WP_Query(); $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
Basically one is copy of the other. Check out
wp-settings.php
, lines 292-305:$GLOBALS['wp_the_query'] = new WP_Query(); $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
$GLOBALS['wp_the_query']
和global $wp_query
之间有什么区别?为什么更喜欢一个?