Skip to content

Commit

Permalink
Merge pull request #2341 from 10up/fix/post-tag-duplication
Browse files Browse the repository at this point in the history
Fix post tag duplication in ES query
  • Loading branch information
oscarssanchez authored Sep 24, 2021
2 parents e39d9c0 + 4aa6265 commit fd70732
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
25 changes: 22 additions & 3 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -899,9 +899,12 @@ public function format_args( $args, $wp_query ) {
}

if ( isset( $args['tag'] ) && ! empty( $args['tag'] ) ) {
if ( ! is_array( $args['tag'] ) && false !== strpos( $args['tag'], ',' ) ) {
$args['tag'] = explode( ',', $args['tag'] );
}
$args['tax_query'][] = array(
'taxonomy' => 'post_tag',
'terms' => array( $args['tag'] ),
'terms' => (array) $args['tag'],
'field' => 'slug',
);
}
Expand Down Expand Up @@ -960,9 +963,25 @@ function( $tax_query ) use ( $args ) {
*/
$taxonomies = get_taxonomies( array(), 'objects' );

/**
* Filter taxonomies to exclude from tax root check.
* Default values prevent duplication of core's default taxonomies post_tag and category in ES query.
*
* @since 3.6.3
* @hook ep_post_tax_excluded_wp_query_root_check
* @param {array} $taxonomies Taxonomies
*/
$excluded_tax_from_root_check = apply_filters(
'ep_post_tax_excluded_wp_query_root_check',
[
'category',
'post_tag',
]
);

foreach ( $taxonomies as $tax_slug => $tax ) {
// Exclude the category taxonomy from this check if we are performing a Tax Query as category_name will be set by core
if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && 'category' !== $tax->name ) {

if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && ! in_array( $tax->name, $excluded_tax_from_root_check, true ) ) {
$args['tax_query'][] = array(
'taxonomy' => $tax_slug,
'terms' => (array) $args[ $tax->query_var ],
Expand Down
43 changes: 43 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4975,6 +4975,49 @@ public function testNestedTaxQuery() {
$this->assertEquals( 2, count( $query->posts ) );
}

/**
* Test a tag query by slug using array and comma separated string as arguments.
*
* @group post
*/
public function testTagSlugQuery() {
$post_id_1 = Functions\create_and_sync_post(
array(
'post_content' => 'findme test 1',
'tags_input' => array( 'slug1', 'slug2' ),
)
);
$post_id_2 = Functions\create_and_sync_post(
array(
'post_content' => 'findme test 2',
'tags_input' => array( 'slug1', 'slug2', 'slug3', 'slug4' ),
)
);

ElasticPress\Elasticsearch::factory()->refresh_indices();

$query1_args = [
's' => 'findme',
'tag' => 'slug1,slug2',
];

$query2_args = [
's' => 'findme',
'tag' => [ 'slug1', 'slug2' ],
];

$query1 = new \WP_Query( $query1_args );
$query2 = new \WP_Query( $query2_args );

$this->assertTrue( isset( $query1->posts[0]->elasticsearch ) );
$this->assertTrue( isset( $query2->posts[0]->elasticsearch ) );

$this->assertEquals( 2, $query1->post_count );
$this->assertEquals( 2, $query1->found_posts );
$this->assertEquals( 2, $query2->post_count );
$this->assertEquals( 2, $query2->found_posts );
}

/**
* Test a query with tag__and and tag_id params
*
Expand Down

0 comments on commit fd70732

Please sign in to comment.