Skip Navigation

[Resolved] Order results by best match

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to order the results of a View based on how well they match the search criteria.

Solution: Views sorting algorithms are based on WordPress sorting algorithms, which do not implement a "best match" criteria. This type of ordering would require significant custom code using the Views APIs wpv_filter_query and/or wpv_filter_query_post_process.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.37 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#916186

Hi, I've a custom search with few taxonomy filters. I need to display all the results that matches with any term of the search, so I've to set an OR relationship btw filters, so if I filter by:

TAX1 term1, term2
TAX2 termA

I want to display this result:

Entry1 (TAX1 term1, term2)
Entry2 (TAX1 term1 - TAX2 termA)
Entry3 (TAX1 term1, term2 - TAX2 termA)
Entry4 (TAX2 termA)

This is what Views do by default.

Now I need to order result by terms matching, so this should be the order of the result:

Entry3 (TAX1 term1, term2 - TAX2 termA)
Entry2 (TAX1 term1 - TAX2 termA)
Entry1 (TAX1 term1, term2)
Entry4 (TAX2 termA)

Is it possible?

Then I also would like to display on each entry in the loop witch terms match with that specific entry. I tryed using:

[wpv-conditional if="( has_term('[wpv-search-term param='wpv-tax1']', 'tax1', null) eq '1' )" ]
[wpv-search-term param='wpv-eating-problem']
[/wpv-conditional]

But this works only if I select 1 term of TAX1, if I select more then one it doesn't.

Is there a solution?

thanks

#916287

Now I need to order result by terms matching,
This might be possible with some custom code using the APIs wpv_filter_query or wpv_filter_query_post_process, but it's not something you can accomplish from wp-admin using Views. Views' sorting algorithm is based on WordPress's WP_Query, which does not offer any Order or OrderBy options based on "best match" of specific taxonomy filters:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
The "relevance" option only applies to text search terms, not taxonomy or custom field searches.

Here's a ticket that uses wpv_filter_query_post_process to inspect the query for taxonomy criteria, then manipulates the query results directly based on those criteria: https://toolset.com/forums/topic/filter-does-not-work-2/#post-556139
It might give you a good idea of how to get started with this custom code. Your solution will be more complex, and isn't something I can offer a quick fix for. More information about the filter API:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

Then I also would like to display on each entry in the loop witch terms match with that specific entry.
This sounds like a separate issue from ordering results. May I kindly ask you to create a separate ticket? This will help us keep the forum organized and help other users find solutions to similar questions.