请注意,没有is_admin() 的方法WP_Query 班
但有趣的是,PHP不会对此抱怨:
$query->is_admin()
在哪里
$query 是的实例
WP_Query.
它总是返回false。
这就是为什么(#src):
/**
* Make private/protected methods readable for backwards compatibility.
*
* @since 4.0.0
* @access public
*
* @param callable $name Method to call.
* @param array $arguments Arguments to pass when calling.
* @return mixed|false Return value of the callback, false otherwise.
*/
public function __call( $name, $arguments ) {
if ( in_array( $name, $this->compat_methods ) ) {
return call_user_func_array( array( $this, $name ), $arguments );
}
return false;
}
这是从
PHP documentation:
__call() 在对象上下文中调用不可访问的方法时触发。
魔术__call 方法,在调用不存在的方法时生效is_admin().
我想你是在勾搭pre_get_posts 行动
我们可以使用is_admin() 检查:
add_action( \'pre_get_posts\', function( \\WP_Query $query )
{
// Only on the front-end
if( is_admin() )
return;
// ... your stuff here ...
} );
或者我们可以使用
is_admin 的公共属性
WP_Query 实例:
add_action( \'pre_get_posts\', function( \\WP_Query $query )
{
// Only on the front-end
if( $query->is_admin )
return;
// ... your stuff here ...
} );
事实上,它是基于
is_admin() 函数,在
parse_query() 方法(
#src):
if ( is_admin() )
$this->is_admin = true;