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

BUG: Using post_tag in query is setting the slug param in a wrong format #2310

Closed
pabamato opened this issue Aug 16, 2021 · 4 comments
Closed
Assignees
Labels
bug Something isn't working high priority
Milestone

Comments

@pabamato
Copy link

Describe the bug

From the WP_Query docs, if you use slugs as the matching param, you can pass params in two formats:

  • Array of slugs
  • String of comma separated slugs

On https://github.com/10up/ElasticPress/blob/trunk/includes/classes/Indexable/Post/Post.php#L844,
The params gets converted to an array, so looks like you can not query for multiple values.

Same wrong format on https://github.com/10up/ElasticPress/blob/trunk/includes/classes/Indexable/Post/Post.php#L852
Also, looks like this is being added with the correct format later on https://github.com/10up/ElasticPress/blob/trunk/includes/classes/Indexable/Post/Post.php#L906
'post_tag' is the query_var for that particular taxonomy

Steps to Reproduce

Query posts passing multiple post_tags:
Having a site with 2 posts(post1, post2) and 2 terms in post_tags(slug1,slug2):
post1 ->slug1
post2 ->slug2

$args_for_multiple_slugs_array_format = [
	'post_tag' => [
		'slug1',
		'slug2',
	],
];
$args_for_multiple_slugs_string_formnat = [
	'post_tag' => 'slug1,slug2',
];
$posts_query = new \WP_Query( $args_for_multiple_slugs_array_format );
$posts_query = new \WP_Query( $args_for_multiple_slugs_string_formnat );

Expected behavior
Both queries should return 2 posts since it should match posts having either slug1 or slug2.

Screenshots

Environment information

  • Device: MacBook
  • OS: MacOS 11.14.0
  • Browser and version: any
  • WordPress version: 5.8
  • ElasticPress version: 3.6.1
  • Elasticsearch version: 5.6.16
  • Where do you host your Elasticsearch server? 10updocker
  • What ElasticPress features do you have currently active? -
  • Plugins and version:N/A
  • Theme and version: N/A
  • Site Health Info:

Additional context

@pabamato pabamato added the bug Something isn't working label Aug 16, 2021
@pabamato
Copy link
Author

Adding additional details for replicating the issue:

  • Default WordPress install with twenty-twentyone theme.
  • Filtering the main query and passing post_tag is causing an error in the ElasticPress endpoint
    If I pass post_tag within 'tax_query', is working as expected

image
image
image

image

@pabamato
Copy link
Author

If that helps to speed-up the debug process, I created a gist having a site export xml file and part of the code I'm using:
https://gist.github.com/pabamato/9825b1c1494eb10f44c4619529196e1b

@mckdemps mckdemps added this to the 3.7.0 milestone Aug 24, 2021
@oscarssanchez
Copy link
Contributor

oscarssanchez commented Aug 31, 2021

Hey @pabamato did

$args_for_multiple_slugs_array_format = [
	'ep_integrate' => true,
	'post_tag' => [
		'slug1',
		'slug2',
	],
];

$posts_query = new \WP_Query( $args_for_multiple_slugs_array_format );

work for you?

When I tested this I got a 400 with this error:

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[terms] unknown token [END_ARRAY] after [terms.post_tag.slug]",
                "line": 1,
                "col": 312
            }
        ],
        "type": "x_content_parse_exception",
        "reason": "[1:312] [bool] failed to parse field [must]",
        "caused_by": {
            "type": "x_content_parse_exception",
            "reason": "[1:312] [bool] failed to parse field [must]",
            "caused_by": {
                "type": "parsing_exception",
                "reason": "[terms] unknown token [END_ARRAY] after [terms.post_tag.slug]",
                "line": 1,
                "col": 312
            }
        }
    },
    "status": 400
}

This seems to be because of bad formatting from the plugin. I just want to check if it is something that recently broke. From the WP_Query docs you linked https://developer.wordpress.org/reference/classes/wp_query/#tag-parameters it seems that the correct param is "tag" right? so something like this:

$args_for_multiple_slugs_array_format = [
	'ep_integrate' => false,
	'tag' => [
		'slug1',
		'slug2',
	]
];

Using post_tag as a query paremeter did not work for me, in this case not using elasticpress, but tag did. I could confirm that a) comma separated string doesn't work, but also that b) an array with more than one element also does not bring the results WP_Query brings

@oscarssanchez
Copy link
Contributor

Once merged #2341 should address the identified problems.

@jeffpaul jeffpaul modified the milestones: 3.7.0, 3.6.3 Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high priority
Projects
None yet
Development

No branches or pull requests

4 participants