Hi,
I have a search page where users can search by custom fields and also there's a free text search option. Relevanssi plugin is installed on the website. If the free text search is used, Toolset is using the Relevanssi to do the search. I can confirm that with a custom Relevanssi filter I created.
When they go to the search page (hidden link), the visitors see all the listings. The current order is by post date but I want to change it so first "Featured events" are listed, and then "active events" and then the rest. Both featured and active events are handled by custom post fields (the former is a checkbox and the latter is a dropdown).
If the free text option is used, I can easily order them in Relevanssi (I did something similar to this: hidden link). If a visitor just visits the search page or filters by custom post fields only, the Relevanssi filter is not called and order doesn't happen.
Is there a way to force the usage of Relevanssi even if I don't use the free text search field? I wouldn't have to worry about keeping two filters up to date and seeing possible unexpected results.
When you leverage Relevanssi for text searches as a View filter, Views hands over the query including the sorting of the results to Relevanssi, to benefit from its search indexes and relevancy ratings. Filtering by custom fields will use the order settings from your View.
Within the UI you are limited in what you can specify as the ordering options, specifically you can only specify up to two parameters to order by, and only the first parameter can include custom fields, the second parameter can only be standard fields such as post date.
It won't interfere with anything you've added to Relevanssi because only one or the other will be operative, depending on whether the text search is being used or not.
Thanks for the assistance Nigel. I have 2 follow up questions.
1) I have written a filter like you suggested, but sorting doesn't work correctly. Is there any issue you can identify in the code below? (it seems like it respects the event status custom field (which can have the values ACTIVE or PAST), but it doesn't respect the featured custom field (which can have the values 0 or 1) or the published date.
2) What if I want to randomise the featured events? featured custom field can have the values of 0 or 1, so random order while querying wouldn't work. I can shuffle them after I fetch them from the database, but what is the right way to do it?
Relevanssi sort is working without an issue as I said in the first message. But I cannot get the sort working for custom post fields when I don't use the text search. My question is about how should I use the "wpv_filter_query". I think it wasn't clear as I didn't add the add_filter method call in my previous message.
I will have to check how exactly you configure you view and where you added the filter and what fields are involved.
Can you please tell me what is your expected result and problem URL where you added your view as well as admin access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
I have sent the login details in the private post.
My expectation is:
if the user didn't use the text search, but only used other filters on the search page; I want to be able to sort the posts by the "featured" field, then the "event status" field, and lastly the post date. If there's a way to randomize the sort for the featured field, that would be even better.