is_main_query()
云策文档标注
概述
is_main_query() 是一个条件函数,用于判断当前查询(如在循环中)是否为 WordPress 的“主查询”,而非自定义或次要查询。它常用于钩子中区分主查询与自定义查询,并返回布尔值。
关键要点
- is_main_query() 是一个无参数函数,通过比较 $wp_query 和 $wp_the_query 对象来确定当前查询是否为主查询。
- 在 'pre_get_posts' 等钩子回调中,应直接使用传递的 WP_Query 对象的 is_main_query() 方法,而非调用此函数,以避免循环调用。
- 函数适用于前端和后台查询,返回 true 表示当前查询是主查询,false 表示不是。
代码示例
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
$query->set( 'cat', '-5' );
}注意事项
- 在 'pre_get_posts' 钩子中,使用 $query->is_main_query() 而非 is_main_query() 函数,以避免错误调用。
- 查询运行前调用此函数会返回 false,并可能触发 _doing_it_wrong() 警告。
原文内容
Determines whether the query is the main query.
Description
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Source
function is_main_query() {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '6.1.0' );
return false;
}
if ( 'pre_get_posts' === current_filter() ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: pre_get_posts, 2: WP_Query->is_main_query(), 3: is_main_query(), 4: Documentation URL. */
__( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
'<code>pre_get_posts</code>',
'<code>WP_Query->is_main_query()</code>',
'<code>is_main_query()</code>',
__( 'https://developer.wordpress.org/reference/functions/is_main_query/' )
),
'3.7.0'
);
}
return $wp_query->is_main_query();
}
Changelog
| Version | Description |
|---|---|
| 3.3.0 | Introduced. |
Skip to note 2 content
Codex
Example of using
WP_Query->is_main_query()instead ofis_main_query()(invalid)/** * If the global query is for a category, exclude category 5. * * @param WP_Query $query Global WP_Query instance. */ function wpdocs_modify_query_exclude_category( $query ) { if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) ) $query->set( 'cat', '-5' ); } add_action( 'pre_get_posts', 'wpdocs_modify_query_exclude_category' );