WebDevStudios / wp-search-with-algolia

Improve search on your site. Autocomplete is included, along with full control over look, feel and relevance.
https://wordpress.org/plugins/wp-search-with-algolia/
141 stars 54 forks source link

Don't replace template on archive pages #401

Closed coreyworrell closed 7 months ago

coreyworrell commented 7 months ago

Describe the bug WordPress allows the search parameter to be used on archive pages and will only search the relevant posts being shown. Example: /custom-post-type-archive/?s=test will show the post type archive template (unless you have a search.php template, but that can be corrected by a filter as well) with those custom posts filtered by the search term. The plugin (if you have the option set to override the search page with InstantSearch) will instead show the instantsearch.php template.

To Reproduce Steps to reproduce the behavior:

  1. Set the option in admin to override the search page with InstantSearch
  2. Go to an archive page (custom post type, term, etc)
  3. Append search query to URL (?s=term)
  4. Notice the archive template is not shown, but rather the Algolia search template

Expected behavior The archive template would still be shown, allowing WordPress to show the archive filtered by the search term.

I think a simple, backwards-compatible solution would be to add a filter to the Algolia_Template_Loader::template_loader() method conditional that would allow an override:

add_filter(
  'algolia_should_load_instantsearch_template',
  function (
    bool $load,
    Algolia_Settings $settings,
    Algolia_Template_Loader $loader,
  ) {
    if (is_archive()) {
      $load = false;
    }

    return $load;
  },
  10,
  3,
);
tw2113 commented 7 months ago

Hi @coreyworrell

Thank you for your feedback and idea.

The template loader function in question can be seen at https://github.com/WebDevStudios/wp-search-with-algolia/blob/2.7.1/includes/class-algolia-template-loader.php#L169-L177 and part of it is going to be the $settings->should_override_search_with_instantsearch() method call.

The reason I bring this up is because that method has its own filter already https://github.com/WebDevStudios/wp-search-with-algolia/blob/2.7.1/includes/class-algolia-settings.php#L332-L336

So something like this below would already be able to be used without any modification to the plugin.

function should_override_instantsearch( $should_override ) {
    if ( is_archive() ) {
        return false;
    }

    return $should_override;
}
add_filter( 'algolia_should_override_search_with_instantsearch', __NAMESPACE__ . '\should_override_instantsearch' );
coreyworrell commented 7 months ago

@tw2113 thanks for the quick reply. That is great. Sorry, I was in a rush and assumed that method just returned the value from the database, not realizing there was already a filter. Thank you very much for the explanation and example.

tw2113 commented 7 months ago

All good. Let us know if you need anything else.