Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix post tag duplication in ES query #2341

Merged
merged 18 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,9 +938,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 @@ -1000,8 +1003,16 @@ function( $tax_query ) use ( $args ) {
$taxonomies = get_taxonomies( array(), 'objects' );

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 ) {
// Prevents duplication of core's default taxonomies post_tag and category in ES query.
apply_filters(
'ep_post_tax_excluded_wp_query_root_check',
$excluded_tax_from_root_check = [
'category',
'post_tag',
]
);
felipeelia marked this conversation as resolved.
Show resolved Hide resolved

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 );
Rahmon marked this conversation as resolved.
Show resolved Hide resolved

$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