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

Default Search Order Should Be An Option #3815

Open
1 task done
realrellek opened this issue Jan 20, 2024 · 0 comments
Open
1 task done

Default Search Order Should Be An Option #3815

realrellek opened this issue Jan 20, 2024 · 0 comments
Milestone

Comments

@realrellek
Copy link

Is your enhancement related to a problem? Please describe.

Hi,

I'm playing around with ElasticPress because bots are spamming our search function with, well, spam and this puts unnecessary pressure on our database.

So if you have it index everything (even protected content), search from within wp-admin is also done using Elastic. Which is nice. What is not so nice is that the default order is by score. I think this is not very intuitive for the editor.

So when playing around, I found that there is a filter, ep_set_default_sort, but that only handles the normal listing of posts. Its default is by post_date desc, which is fine because that is how WP defaults too.

When you actually do a search, you need to hook into ep_set_sort. And it is not very obvious what you have to return. For Google: This would be the answer:

add_filter( 'ep_set_sort', 'rellek_ep_sort_results_by_date_desc', 10, 2 );
function rellek_ep_sort_results_by_date_desc($sort, $order)
{
    return array(
        'post_date' => array(
            'order' => 'DESC',
        ),
    );
}

Well and I think this should be an UI option. Because first of all, you find all sorts of confusing answers to this problem (maybe leftovers from older versions) with wrong information - such as you need to return "date" (while for this hook, you need to return "post_date"). And then I feel like you could offer all the other possible values, like score.

Designs

No response

Describe alternatives you've considered

I guess adding this filter see above does the trick... For now. But when you change your internal data structure, it might stop working. And being a filter, it also would not throw an error, so it'd be hard to debug why the behaviour changes.

Also WP's default order (if nothing else is specified) is by post_title (if the queried word is included) and by post_date. So having the default changed to at least post_date desc would come closer to what stock WP does.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@felipeelia felipeelia added this to the 5.1.0 milestone Feb 6, 2024
@felipeelia felipeelia modified the milestones: 5.1.0, 5.2.0 Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants