diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php
index 6ff2d2d9fd..9fd70f48cb 100644
|
a
|
b
|
function wp_ajax_query_attachments() {
|
| 2975 | 2975 | |
| 2976 | 2976 | // Filter query clauses to include filenames. |
| 2977 | 2977 | if ( isset( $query['s'] ) ) { |
| 2978 | | add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
| | 2978 | add_filter( 'posts_clauses', '_filter_query_attachment_meta' ); |
| 2979 | 2979 | } |
| 2980 | 2980 | |
| 2981 | 2981 | /** |
diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php
index 77aadb2135..0be860ea01 100644
|
a
|
b
|
function wp_edit_attachments_query_vars( $q = false ) {
|
| 1269 | 1269 | |
| 1270 | 1270 | // Filter query clauses to include filenames. |
| 1271 | 1271 | if ( isset( $q['s'] ) ) { |
| 1272 | | add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
| | 1272 | add_filter( 'posts_clauses', '_filter_query_attachment_meta' ); |
| 1273 | 1273 | } |
| 1274 | 1274 | |
| 1275 | 1275 | return $q; |
diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
index a5bd9678bb..ba80b0111c 100644
|
a
|
b
|
function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
|
| 7482 | 7482 | } |
| 7483 | 7483 | |
| 7484 | 7484 | /** |
| 7485 | | * Filters the SQL clauses of an attachment query to include filenames. |
| | 7485 | * Filters the SQL clauses of an attachment query to include meta (filename/alt). |
| 7486 | 7486 | * |
| 7487 | 7487 | * @since 4.7.0 |
| 7488 | 7488 | * @access private |
| … |
… |
function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
|
| 7493 | 7493 | * DISTINCT, fields (SELECT), and LIMITS clauses. |
| 7494 | 7494 | * @return string[] The modified array of clauses. |
| 7495 | 7495 | */ |
| 7496 | | function _filter_query_attachment_filenames( $clauses ) { |
| | 7496 | function _filter_query_attachment_meta( $clauses ) { |
| 7497 | 7497 | global $wpdb; |
| 7498 | 7498 | remove_filter( 'posts_clauses', __FUNCTION__ ); |
| 7499 | 7499 | |
| 7500 | 7500 | // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs. |
| 7501 | | $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; |
| | 7501 | $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND ( sq1.meta_key = '_wp_attached_file' OR sq1.meta_key = '_wp_attachment_image_alt' ) )"; |
| 7502 | 7502 | |
| 7503 | 7503 | $clauses['groupby'] = "{$wpdb->posts}.ID"; |
| 7504 | 7504 | |
diff --git a/tests/phpunit/tests/query/search.php b/tests/phpunit/tests/query/search.php
index c1c74c8240..38af8fb872 100644
|
a
|
b
|
class Tests_Query_Search extends WP_UnitTestCase {
|
| 455 | 455 | ); |
| 456 | 456 | |
| 457 | 457 | add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true ); |
| 458 | | add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
| | 458 | add_filter( 'posts_clauses', '_filter_query_attachment_meta' ); |
| 459 | 459 | |
| 460 | 460 | // Pass post_type a string value. |
| 461 | 461 | $q = new WP_Query( |
| … |
… |
class Tests_Query_Search extends WP_UnitTestCase {
|
| 485 | 485 | ); |
| 486 | 486 | |
| 487 | 487 | add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true ); |
| 488 | | add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
| | 488 | add_filter( 'posts_clauses', '_filter_query_attachment_meta' ); |
| 489 | 489 | |
| 490 | 490 | // Pass post_type an array value. |
| 491 | 491 | $q = new WP_Query( |
| … |
… |
class Tests_Query_Search extends WP_UnitTestCase {
|
| 544 | 544 | |
| 545 | 545 | add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true ); |
| 546 | 546 | add_post_meta( $attachment, '_test_meta_key', 'value', true ); |
| 547 | | add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
| | 547 | add_filter( 'posts_clauses', '_filter_query_attachment_meta' ); |
| 548 | 548 | |
| 549 | 549 | // Pass post_type a string value. |
| 550 | 550 | $q = new WP_Query( |
| … |
… |
class Tests_Query_Search extends WP_UnitTestCase {
|
| 584 | 584 | wp_set_post_terms( $attachment, 'test', 'post_tag' ); |
| 585 | 585 | |
| 586 | 586 | add_post_meta( $attachment, '_wp_attached_file', 'some-image5.png', true ); |
| 587 | | add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
| | 587 | add_filter( 'posts_clauses', '_filter_query_attachment_meta' ); |
| 588 | 588 | |
| 589 | 589 | // Pass post_type a string value. |
| 590 | 590 | $q = new WP_Query( |
| … |
… |
class Tests_Query_Search extends WP_UnitTestCase {
|
| 606 | 606 | $this->assertSame( array( $attachment ), $q->posts ); |
| 607 | 607 | } |
| 608 | 608 | |
| | 609 | /** |
| | 610 | * @ticket 39004 |
| | 611 | */ |
| | 612 | public function test_include_file_names_in_attachment_search_with_alt_text() { |
| | 613 | $attachment = self::factory()->post->create( |
| | 614 | array( |
| | 615 | 'post_type' => 'attachment', |
| | 616 | 'post_status' => 'publish', |
| | 617 | 'post_title' => 'bar foo', |
| | 618 | 'post_content' => 'foo bar', |
| | 619 | 'post_excerpt' => 'This post has foo', |
| | 620 | ) |
| | 621 | ); |
| | 622 | |
| | 623 | add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true ); |
| | 624 | |
| | 625 | // Add an alt text |
| | 626 | add_post_meta( $attachment, '_wp_attachment_image_alt', 'Alt text', true ); |
| | 627 | |
| | 628 | add_filter( 'posts_clauses', '_filter_query_attachment_meta' ); |
| | 629 | |
| | 630 | // Pass post_type a string value. |
| | 631 | $q = new WP_Query( |
| | 632 | array( |
| | 633 | 's' => 'Alt text', |
| | 634 | 'fields' => 'ids', |
| | 635 | 'post_type' => 'attachment', |
| | 636 | 'post_status' => 'inherit', |
| | 637 | ) |
| | 638 | ); |
| | 639 | |
| | 640 | $this->assertSame( array( $attachment ), $q->posts ); |
| | 641 | } |
| | 642 | |
| 609 | 643 | /** |
| 610 | 644 | * @ticket 22744 |
| 611 | 645 | */ |
| 612 | | public function test_filter_query_attachment_filenames_unhooks_itself() { |
| 613 | | add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
| | 646 | public function test_filter_query_attachment_meta_unhooks_itself() { |
| | 647 | add_filter( 'posts_clauses', '_filter_query_attachment_meta' ); |
| 614 | 648 | |
| 615 | 649 | apply_filters( |
| 616 | 650 | 'posts_clauses', |
| … |
… |
class Tests_Query_Search extends WP_UnitTestCase {
|
| 625 | 659 | ) |
| 626 | 660 | ); |
| 627 | 661 | |
| 628 | | $result = has_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
| | 662 | $result = has_filter( 'posts_clauses', '_filter_query_attachment_meta' ); |
| 629 | 663 | |
| 630 | 664 | $this->assertFalse( $result ); |
| 631 | 665 | } |