split_the_query
云策文档标注
概述
split_the_query 过滤器用于控制 WP_Query 是否拆分查询,以优化性能。拆分查询会先获取帖子ID,再逐个获取完整帖子数据,而不是一次性获取所有数据行。
关键要点
- 过滤器名称:split_the_query
- 作用:决定是否拆分查询,影响查询性能和数据获取方式
- 参数:$split_the_query(布尔值,是否拆分)、$query(WP_Query 实例)、$old_request(过滤前的完整SQL查询)、$clauses(查询子句的关联数组)
- 相关函数:WP_Query::get_posts()
- 版本变化:6.6.0 版本添加了 $old_request 和 $clauses 参数,3.4.0 版本引入
注意事项
使用此过滤器时需谨慎,拆分查询可能适用于特定场景,如处理大量数据时优化内存使用。参考 Trac 票证 #57296 了解可能的用例。
原文内容
Filters whether to split the query.
Description
Splitting the query will cause it to fetch just the IDs of the found posts (and then individually fetch each post by ID), rather than fetching every complete row at once. One massive result vs. many small results.
Parameters
$split_the_querybool-
Whether or not to split the query.
$queryWP_Query-
The WP_Query instance.
$old_requeststring-
The complete SQL query before filtering.
$clausesstring[]-
Associative array of the clauses for the query.
wherestringThe WHERE clause of the query.groupbystringThe GROUP BY clause of the query.joinstringThe JOIN clause of the query.orderbystringThe ORDER BY clause of the query.distinctstringThe DISTINCT clause of the query.fieldsstringThe SELECT clause of the query.limitsstringThe LIMIT clause of the query.
Source
$split_the_query = apply_filters( 'split_the_query', $split_the_query, $this, $old_request, compact( $pieces ) );
Skip to note 2 content
Till Krüss
See https://core.trac.wordpress.org/ticket/57296 for possible use-case.