fix: media search also matches file names

This commit is contained in:
2026-09-26 05:05:10 +02:00
parent 28bb033a8b
commit 9634ca740d
2 changed files with 54 additions and 2 deletions
+20 -2
View File
@@ -208,7 +208,7 @@ final class MediaSearchAbility implements Ability
*/
private function searchPage(array $args, int $limit, int $offset): array
{
$rows = get_posts(array_merge($args, ['posts_per_page' => $limit + 1, 'offset' => $offset]));
$rows = self::query(array_merge($args, ['posts_per_page' => $limit + 1, 'offset' => $offset]));
$page = Cursor::page(array_values($rows), $limit, $offset);
return [
@@ -230,7 +230,7 @@ final class MediaSearchAbility implements Ability
*/
private function unusedPage(array $args, int $limit, int $offset): array
{
$candidates = array_values(get_posts(array_merge($args, [
$candidates = array_values(self::query(array_merge($args, [
'posts_per_page' => self::MAX_SCAN + 1,
'offset' => $offset,
])));
@@ -282,4 +282,22 @@ final class MediaSearchAbility implements Ability
{
return current_user_can('edit_post', (int) $attachment->ID);
}
/**
* get_posts() with file-name matching for a search term, as in the media library screen:
* WordPress only searches _wp_attached_file when wp_allow_query_attachment_by_filename is
* true. WP_Query reads that filter once per query and then calls remove_all_filters() on it
* itself, so it applies to this query only.
*
* @param array<string, mixed> $args
* @return array<int|string, mixed>
*/
private static function query(array $args): array
{
if (isset($args['s'])) {
add_filter('wp_allow_query_attachment_by_filename', '__return_true');
}
return get_posts($args);
}
}