Thread Starter
Андрій
(@andxbes)
I would also like to add sorting by metafield, for example this code works correctly for me
<?php
add_filter('posts_orderby', function ($orderby, $query) {
if (!is_admin() && $query->is_main_query() && $query->is_search() && class_exists('AcfBetterSearch\AcfBetterSearch')) {
$s = esc_sql($query->get('s'));
$orderby = "CASE WHEN a.meta_value LIKE '%{$s}%' THEN 1 ELSE 2 END, $orderby";
}
return $orderby;
}, 10, 2);
First it returns complete matches, and then by word.
-
This reply was modified 2 years, 7 months ago by
Андрій.
-
This reply was modified 2 years, 7 months ago by
Андрій.
-
This reply was modified 2 years, 7 months ago by
Андрій.
Hi @andxbes,
Thank you for your message.
Search works by either searching for the entire sentence (in the order entered in the search field) or all words from the sentence entered in the search field. Standard search in WordPress works in a similar way – i.e. if you enter 3 words in the search field, all these words must be in a given post. They may be in a different order, but they must be there. We relied on the search engine in WordPress, which is why the functionality works the way it does.
Best,
Mateusz
Thread Starter
Андрій
(@andxbes)
In fact, the WP itself searches for both whole phrases and words, and makes relevant results, posts with whole phrases have the highest order, and posts with partially found words have a lower order WP_Query::parse_search_order()
Thread Starter
Андрій
(@andxbes)
After installing your plugin, I came across the fact that it doesn’t quite search using custom fields the way the WP itself does using standard fields. But with a little modification you can achieve an acceptable result. In any case, thanks for your work.