Make WordPress Core

Ticket #22744: 22744.12.diff

File 22744.12.diff, 3.3 KB (added by joemcgill, 8 years ago)
  • src/wp-admin/includes/ajax-actions.php

    diff --git src/wp-admin/includes/ajax-actions.php src/wp-admin/includes/ajax-actions.php
    index 170eb96..0992aa0 100644
    function wp_ajax_query_attachments() { 
    23982398                $query['post_status'] .= ',private';
    23992399
    24002400        // Filter query clauses to include filenames.
    2401         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     2401        if ( isset( $query['s'] ) ) {
     2402                add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     2403        }
    24022404
    24032405        /**
    24042406         * Filters the arguments passed to WP_Query during an Ajax
  • src/wp-admin/includes/post.php

    diff --git src/wp-admin/includes/post.php src/wp-admin/includes/post.php
    index b12aa5d..c352cbc 100644
    function wp_edit_attachments_query_vars( $q = false ) { 
    11451145        }
    11461146
    11471147        // Filter query clauses to include filenames.
    1148         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     1148        if ( isset( $q['s'] ) ) {
     1149                add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     1150        }
    11491151
    11501152        return $q;
    11511153}
    function _filter_query_attachment_filenames( $clauses ) { 
    11641166        global $wpdb;
    11651167        remove_filter( 'posts_clauses', __FUNCTION__ );
    11661168
    1167         $clauses['join'] = " INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id )";
     1169        // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs.
     1170        $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )";
     1171
    11681172        $clauses['groupby'] = "{$wpdb->posts}.ID";
    11691173
    11701174        $clauses['where'] = preg_replace(
    11711175                "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/",
    1172                 "$0 OR ( {$wpdb->postmeta}.meta_key = '_wp_attached_file' AND {$wpdb->postmeta}.meta_value $1 $2 )",
     1176                "$0 OR ( sq1.meta_value $1 $2 )",
    11731177                $clauses['where'] );
    11741178
    11751179        return $clauses;
  • tests/phpunit/tests/query/search.php

    diff --git tests/phpunit/tests/query/search.php tests/phpunit/tests/query/search.php
    index d0a34a6..9571733 100644
    class Tests_Query_Search extends WP_UnitTestCase { 
    384384                $this->assertNotEquals( array( $attachment ), $q->posts );
    385385        }
    386386
     387        /**
     388         * @ticket 22744
     389         */
     390        public function test_include_file_names_in_attachment_search_with_meta_query() {
     391                $attachment = self::factory()->post->create( array(
     392                        'post_type'    => 'attachment',
     393                        'post_status'  => 'publish',
     394                        'post_title'   => 'bar foo',
     395                        'post_content' => 'foo bar',
     396                        'post_excerpt' => 'This post has foo',
     397                ) );
     398
     399                add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true );
     400                add_post_meta( $attachment, '_test_meta_key', 'value', true );
     401                add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     402
     403                // Pass post_type a string value.
     404                $q = new WP_Query( array(
     405                        's'           => 'image4',
     406                        'fields'      => 'ids',
     407                        'post_type'   => 'attachment',
     408                        'post_status' => 'inherit',
     409                        'meta_query'  => array(
     410                                array(
     411                                        'key'     => '_test_meta_key',
     412                                        'value'   => 'value',
     413                                        'compare' => '=',
     414                                ),
     415                        ),
     416                ) );
     417
     418                $this->assertSame( array( $attachment ), $q->posts );
     419        }
     420
    387421        public function filter_posts_search( $sql ) {
    388422                return $sql . ' /* posts_search */';
    389423        }