10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.24k stars 312 forks source link

BUG: Using 's' parameter in WP_Query and a single custom post_type as string instead of array, also searches for attachment post_type when Documents feature is active #3884

Closed eartahhj closed 5 months ago

eartahhj commented 5 months ago

Describe the bug

Goodmorning, I am testing EP and I am not 100% sure this is a bug, but the behaviour is very weird and I'd like to report it as a bug. Feel free to correct me and/or close it if I am wrong.

So I have activated the ElasticPress plugin on a development Wordpress site, and I have an archive template where I need to retrieve "events" based on a search form inside this template.

I am not using the default 's' parameter in the GET query string, but I am using the default 's' in the WP_Query args.

$searchQuery = $_GET['searchEvents'] ?? '';
$eventsQueryArgs = [
    'post_type' => 'event',
    'paged' => $paged
];
$eventsQueryArgs['s'] = $searchQuery;
$eventsQueryArgs['search_columns'] = ['post_title'];

If I print_r the Query like this, I can see:

[query_vars] => Array
        (
            [post_type] => Array
                (
                    [0] => event
                    [1] => attachment
                )

If I disable the ElasticPress plugin OR if I disable the Documents feature OR if I set the post_type argument as array instead of string:

$eventsQueryArgs = [
    'post_type' => [
        'event'
    ],
    'paged' => $paged
];

The result is different, the attachment post type is not present in the query vars anymore, as expected:

   [query_vars] => Array
        (
            [post_type] => Array
                (
                    [0] => event
                )

As you can see, [1] => attachment is not there anymore.

I do not expect to get attachments if I search for a single post_type as a string, it generates a weird behaviour in my opinion.

Let me know if you can reproduce the same behaviour as explained here. Thank you

Steps to Reproduce

  1. Create a WP_Query with the 's' parameter in the args and a post_type set to a single post type with a string (not array)
  2. Enable the Documents feature in EP
  3. print_r the query and check query_vars, it should have attachment inside the post_type
  4. Now try either options: set post_type as an array with one element, OR disable EP plugin, OR disable the Documents feature
  5. print_r the query and check query_vars again, attachment should be gone from the post_type

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress and ElasticPress information

WP 6.4.3, EP 5.0.2

Code of Conduct