筛选附件查询的SQL子句以包含文件名。

原型

_filter_query_attachment_filenames( array $clauses )

参数

$clauses (array) (Required) 一个数组,包括WHERE,GROUP BY,JOIN,ORDER BY,DISTINCT,fields(SELECT)和LIMITS子句。

返回值

(array)  修改后的条款。

源文件

路径:wp-includes/post.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
...
function _filter_query_attachment_filenames( $clauses ) {
	global $wpdb;
	remove_filter( 'posts_clauses', __FUNCTION__ );

	// Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs.
	$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";

	$clauses['groupby'] = "{$wpdb->posts}.ID";

	$clauses['where'] = preg_replace(
		"/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/",
		"$0 OR ( sq1.meta_value $1 $2 )",
		$clauses['where'] );

	return $clauses;
}

...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/_filter_query_attachment_filenames/