Make WordPress Core

Changeset 36647


Ignore:
Timestamp:
02/23/2016 08:13:22 PM (9 years ago)
Author:
boonebgorges
Message:

Query: Search should match post_excerpt in addition to title and content.

When ordering search results, exact matches in the post excerpt are weighted
above those in post content, but below those in the post title.

Props swissspidy, sebastian.pisula.
FIxes #35762.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r36632 r36647  
    21572157
    21582158            $like = $n . $wpdb->esc_like( $term ) . $n;
    2159             $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like );
     2159            $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like );
    21602160            $searchand = ' AND ';
    21612161        }
     
    22812281            }
    22822282
    2283             // sentence match in 'post_content'
     2283            // Sentence match in 'post_content' and 'post_excerpt'.
    22842284            if ( $like ) {
    2285                 $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
     2285                $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_excerpt LIKE %s THEN 4 ", $like );
     2286                $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 5 ", $like );
    22862287            }
    22872288
    22882289            if ( $search_orderby ) {
    2289                 $search_orderby = '(CASE ' . $search_orderby . 'ELSE 5 END)';
     2290                $search_orderby = '(CASE ' . $search_orderby . 'ELSE 6 END)';
    22902291            }
    22912292        } else {
  • trunk/tests/phpunit/tests/query/search.php

    r36520 r36647  
    179179    }
    180180
     181    /**
     182     * @ticket 35762
     183     */
     184    public function test_search_post_excerpt() {
     185        $p1 = self::factory()->post->create( array(
     186            'post_status' => 'publish',
     187            'post_content' => 'This post has foo but also bar',
     188        ) );
     189        $p2 = self::factory()->post->create( array(
     190            'post_status' => 'publish',
     191            'post_content' => '',
     192            'post_excerpt' => 'This post has bar and baz',
     193        ) );
     194        $p3 = self::factory()->post->create( array(
     195            'post_status' => 'publish',
     196            'post_content' => '',
     197            'post_excerpt' => 'This post has only foo',
     198        ) );
     199
     200        $q = new WP_Query( array(
     201            's' => 'foo',
     202            'fields' => 'ids',
     203        ) );
     204
     205        $this->assertEqualSets( array( $p1, $p3 ), $q->posts );
     206
     207        $q = new WP_Query( array(
     208            's' => 'bar',
     209            'fields' => 'ids',
     210        ) );
     211
     212        $this->assertEqualSets( array( $p1, $p2 ), $q->posts );
     213
     214        $q = new WP_Query( array(
     215            's' => 'baz',
     216            'fields' => 'ids',
     217        ) );
     218
     219        $this->assertEqualSets( array( $p2 ), $q->posts );
     220    }
     221
     222    /**
     223     * @ticket 35762
     224     */
     225    public function test_search_order_title_before_excerpt_and_content() {
     226        $p1 = self::factory()->post->create( array(
     227            'post_status' => 'publish',
     228            'post_title'  => 'This post has foo',
     229            'post_content' => '',
     230            'post_excerpt' => '',
     231        ) );
     232
     233        $p2 = self::factory()->post->create( array(
     234            'post_status'  => 'publish',
     235            'post_title' => '',
     236            'post_content' => 'This post has foo',
     237            'post_excerpt' => '',
     238        ) );
     239
     240        $p3 = self::factory()->post->create( array(
     241            'post_status'  => 'publish',
     242            'post_title' => '',
     243            'post_content' => '',
     244            'post_excerpt' => 'This post has foo',
     245        ) );
     246
     247        $q = new WP_Query( array(
     248            's'      => 'this post has foo',
     249            'fields' => 'ids',
     250            'orderby' => false,
     251        ) );
     252
     253        $this->assertSame( array( $p1, $p3, $p2 ), $q->posts );
     254    }
     255
    181256    public function filter_posts_search( $sql ) {
    182257        return $sql . ' /* posts_search */';
Note: See TracChangeset for help on using the changeset viewer.