如果要测试自定义SQL以查看其对加载时间的影响,可以尝试以下查询交换:
/**
* Restrict the potential slow query in the meta_form() to the current post ID.
*
* @see http://wordpress.stackexchange.com/a/187712/26350
*/
add_action( \'add_meta_boxes_post\', function( $post )
{
add_filter( \'query\', function( $sql ) use ( $post )
{
global $wpdb;
$find = "SELECT meta_key
FROM $wpdb->postmeta
GROUP BY meta_key
HAVING meta_key NOT LIKE \'\\\\\\_%\'
ORDER BY meta_key
LIMIT 30";
if( preg_replace( \'/\\s+/\', \' \', $sql ) === preg_replace( \'/\\s+/\', \' \', $find )
&& $post instanceof WP_Post
) {
$post_id = (int) $post->ID;
$sql = "SELECT meta_key
FROM $wpdb->postmeta
WHERE post_id = {$post_id}
GROUP BY meta_key
HAVING meta_key NOT LIKE \'\\\\\\_%\'
ORDER BY meta_key
LIMIT 30";
}
return $sql;
} );
} );
这里我们使用
add_meta_boxes_{$post_type}
钩子,在哪里
$post_type = \'post\'
.
这里我们交换了整个查询,但我们也可以调整它以支持动态限制。
希望您可以根据自己的需要进行调整。
更新:
此可能较慢的SQL核心查询现已在WP版本4.3中进行了调整,从
SELECT meta_key
FROM wp_postmeta
GROUP BY meta_key
HAVING meta_key NOT LIKE \'\\\\_%\'
ORDER BY meta_key
LIMIT 30
收件人:
SELECT DISTINCT meta_key
FROM wp_postmeta
WHERE meta_key NOT BETWEEN \'_\' AND \'_z\'
HAVING meta_key NOT LIKE \'\\_%\'
ORDER BY meta_key
LIMIT 30;
查看核心票据
#24498 了解更多信息。