Make WordPress Core

Ticket #22744: 22744.5.diff

File 22744.5.diff, 3.2 KB (added by swissspidy, 8 years ago)
  • src/wp-includes/class-wp-query.php

    diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php
    index 9590406..03c9ff2 100644
    class WP_Query { 
    13411341                        }
    13421342
    13431343                        $like = $n . $this->db->esc_like( $term ) . $n;
    1344                         $search .= $this->db->prepare( "{$searchand}(({$this->db->posts}.post_title $like_op %s) $andor_op ({$this->db->posts}.post_excerpt $like_op %s) $andor_op ({$this->db->posts}.post_content $like_op %s))", $like, $like, $like );
     1344
     1345                        // If this is a search for attachments, inlcude the filename in the search.
     1346                        if ( array( 'attachment' ) === (array) $q['post_type'] ) {
     1347                                $search .= $this->db->prepare( "{$searchand}(({$this->db->posts}.post_title $like_op %s) $andor_op ({$this->db->posts}.post_excerpt $like_op %s) $andor_op ({$this->db->posts}.post_content $like_op %s) $andor_op ({$this->db->postmeta}.meta_key = '_wp_attached_file' AND {$this->db->postmeta}.meta_value $like_op %s))", $like, $like, $like, $like );
     1348                        } else {
     1349                                $search .= $this->db->prepare( "{$searchand}(({$this->db->posts}.post_title $like_op %s) $andor_op ({$this->db->posts}.post_excerpt $like_op %s) $andor_op ({$this->db->posts}.post_content $like_op %s))", $like, $like, $like );
     1350                        }
     1351
    13451352                        $searchand = ' AND ';
    13461353                }
    13471354
    class WP_Query { 
    20682075                        }
    20692076                }
    20702077
    2071                 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
     2078                if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) || ( $this->is_search && array( 'attachment' ) === (array) $q['post_type'] ) ) {
    20722079                        $groupby = "{$this->db->posts}.ID";
    20732080                }
    20742081
    class WP_Query { 
    21172124                }
    21182125                $where .= $search . $whichauthor . $whichmimetype;
    21192126
     2127                // Modify the JOIN clause for attachment searches.
     2128                if ( $this->is_search && array( 'attachment' ) === (array) $q['post_type'] ) {
     2129                        $join .= " INNER JOIN {$this->db->postmeta} ON ( {$this->db->posts}.ID = {$this->db->postmeta}.post_id )";
     2130                }
     2131
    21202132                if ( ! empty( $this->meta_query->queries ) ) {
    21212133                        $clauses = $this->meta_query->get_sql( 'post', $this->db->posts, 'ID', $this );
    21222134                        $join   .= $clauses['join'];
  • tests/phpunit/tests/query/search.php

    diff --git tests/phpunit/tests/query/search.php tests/phpunit/tests/query/search.php
    index a228f91..15d2193 100644
    class Tests_Query_Search extends WP_UnitTestCase { 
    280280                $this->assertSame( array( $p1, $p3, $p2 ), $q->posts );
    281281        }
    282282
     283        /**
     284         * @ticket 22744
     285         */
     286        public function test_s_should_respect_attachment_file_names() {
     287                $attachment = self::factory()->post->create( array(
     288                        'post_type'    => 'attachment',
     289                        'post_status'  => 'publish',
     290                        'post_title'   => 'bar foo',
     291                        'post_content' => 'foo bar',
     292                        'post_excerpt' => 'This post has foo',
     293                ) );
     294
     295                add_post_meta( $attachment, '_wp_attached_file', 'some-image.png', true );
     296
     297                $q = new WP_Query( array(
     298                        's'           => 'image',
     299                        'fields'      => 'ids',
     300                        'post_type'   => array( 'attachment' ),
     301                        'post_status' => 'inherit',
     302                ) );
     303
     304                $this->assertSame( array( $attachment ), $q->posts );
     305        }
     306
    283307        public function filter_posts_search( $sql ) {
    284308                return $sql . ' /* posts_search */';
    285309        }