Elasticpress: Facets widget removes empty search query parameter

Created on 26 Jul 2019  路  4Comments  路  Source: 10up/ElasticPress

Describe the bug
Facets removes an empty search parameter from the URL when creating links via build_query_url() causing the facet widget links to become invalid.

Steps to Reproduce

  1. Go to your site's search page without a search query: https://example.com/?s
  2. Click on a category link in the facets widget
  3. Facets widget sends you to your home page: https://example.com/?filter_category=my-cool-category

Expected behavior
To maintain the proper context, the facets build_query_url() method should not remove the empty search parameter.

Debatable if you consider https://example.com/?s to be a page a user should never be on.

needs documentation

Most helpful comment

For anyone needing facet support with empty searches, you can utilize the existing ep_facet_query_string filter to replace the 's' query var after it's removed by the 'build_query_string' method mentioned earlier:

add_filter( 'ep_facet_query_string', function( $query_string ) {
    $query_vars = parse_str( $query_string, $vars );

    // replace 's' query var is missing from the original query string
    if ( ! isset( $vars['s'] ) ) {
        return add_query_arg( 's', '', $query_string );
    }

    return $query_string;
} );

All 4 comments

Hi @barryceelen,

If the s parameter is not removed, then the query would look like this: https://example.com/?sfilter_category=my-cool-category or https://example.com/?s=filter_category=my-cool-category

In the first case, the query would make no sense. On the second one, it will look for filter_category=my-cool-category literal string. I think the problem was that faceting was not working before. But it seems this is working correctly now with the latest fixes.

If you go to the empty search now, you are able to facet even with an empty search. Was this the problem and if not, could you describe how the faceting should work in your opinion for this case?

Thanks!

Hi @oscarssanchez , thanks for your reply.

With an empty search query, the URL would not look like either one of your examples (you missed the &), but like this:

https://example.com/?s&filter_category=my-cool-category

WordPress uses the s query parameter to determine whether is_search() returns true, and also whether to load the search.php template.

This for example is a valid URL (keeping the user in the context of the search page):

http://example.com/?s=&cat=my-cool-category

When the facet widget is used on the search page and the user performs a faceted search while the search phrase is empty, the search.php template will not be loaded as the empty s parameter will be removed from the URL by Elasticpress. This can be a problem eg. when using a specific layout for the search page.

For our needs we can solve the issue of the search template not being loaded by looking at ep_facet being present in $wp_query and jumping through some hoops.

However, I think Elasticpress should not break existing core behaviour by removing the empty search query parameter if there is one present.

For anyone needing facet support with empty searches, you can utilize the existing ep_facet_query_string filter to replace the 's' query var after it's removed by the 'build_query_string' method mentioned earlier:

add_filter( 'ep_facet_query_string', function( $query_string ) {
    $query_vars = parse_str( $query_string, $vars );

    // replace 's' query var is missing from the original query string
    if ( ! isset( $vars['s'] ) ) {
        return add_query_arg( 's', '', $query_string );
    }

    return $query_string;
} );

Thanks everyone for your contribution & suggestions to this ticket. We are going to document this under Develop Support. You'll see that live soon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jakejackson1 picture jakejackson1  路  8Comments

vnmedeiros picture vnmedeiros  路  3Comments

psorensen picture psorensen  路  7Comments

tamara-m picture tamara-m  路  3Comments

MediaMaquina picture MediaMaquina  路  5Comments