Make WordPress Core


Ignore:
Timestamp:
10/08/2015 03:17:22 AM (8 years ago)
Author:
boonebgorges
Message:

Allow excluded keywords when searching posts.

Pass a keyword with a leading hyphen to exclude posts containing that keyword.
For example, 'taco -onions' will return posts that contain the word 'taco' but
do not contain the word 'onions'.

Props akibjorklund.
Fixes #33988.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/search.php

    r31622 r34934  
    5959        return array();
    6060    }
     61
     62    /**
     63     * @ticket 33988
     64     */
     65    public function test_s_should_exclude_term_prefixed_with_dash() {
     66        $p1 = $this->factory->post->create( array(
     67            'post_status' => 'publish',
     68            'post_content' => 'This post has foo but also bar',
     69        ) );
     70        $p2 = $this->factory->post->create( array(
     71            'post_status' => 'publish',
     72            'post_content' => 'This post has only foo',
     73        ) );
     74
     75        $q = new WP_Query( array(
     76            's' => 'foo -bar',
     77            'fields' => 'ids',
     78        ) );
     79
     80        $this->assertEqualSets( array( $p2 ), $q->posts );
     81    }
     82
     83    /**
     84     * @ticket 33988
     85     */
     86    public function test_s_should_exclude_first_term_if_prefixed_with_dash() {
     87        $p1 = $this->factory->post->create( array(
     88            'post_status' => 'publish',
     89            'post_content' => 'This post has foo but also bar',
     90        ) );
     91        $p2 = $this->factory->post->create( array(
     92            'post_status' => 'publish',
     93            'post_content' => 'This post has only bar',
     94        ) );
     95
     96        $q = new WP_Query( array(
     97            's' => '-foo bar',
     98            'fields' => 'ids',
     99        ) );
     100
     101        $this->assertEqualSets( array( $p2 ), $q->posts );
     102    }
     103
     104    /**
     105     * @ticket 33988
     106     */
     107    public function test_s_should_not_exclude_for_dashes_in_the_middle_of_words() {
     108        $p1 = $this->factory->post->create( array(
     109            'post_status' => 'publish',
     110            'post_content' => 'This post has foo but also bar',
     111        ) );
     112        $p2 = $this->factory->post->create( array(
     113            'post_status' => 'publish',
     114            'post_content' => 'This post has only bar',
     115        ) );
     116        $p3 = $this->factory->post->create( array(
     117            'post_status' => 'publish',
     118            'post_content' => 'This post has only foo-bar',
     119        ) );
     120
     121        $q = new WP_Query( array(
     122            's' => 'foo-bar',
     123            'fields' => 'ids',
     124        ) );
     125
     126        $this->assertEqualSets( array( $p3 ), $q->posts );
     127    }
    61128}
Note: See TracChangeset for help on using the changeset viewer.