Hi,
I´ve created a custom search, where I can select the region. The custom post type can has one region. Now I want to order my view results by the region, which was selected on the custom search to show this results first by sorting alphabetically.
How can I do this? The ordering option at the view can "only" order by a custom field, but I cannot give any priority to a selected search parameter.
Regards,
Hello,
Please elaborate the question with more details:
but I cannot give any priority to a selected search parameter
How do you setup the "region"?
If it is a custom taxonomy, then you can not order the post view's result by custom taxonomy, since Views plugin is using wordpress Class WP_Query to query the posts, there isn't option to orderby taxonomy:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
In your case, I suggest you setup the "region" as a custom select field, then you will be able to order the result by the custom select field, and filter the result by the custom select field:
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/
Hi,
the following I try to solve:
At the search I select a region and every custom post type can have multiple regions assigned (custom taxonomy). Now I want to show these results first, which have the selected region in their address (home office). This field is a custom field.
Can I match selected search field (region) with a custom field region and priorize / weight this at the search results to be displayed first, when both fields are the same?
Regards,
Thanks for the details, there isn't such kind of built-in feature within Views plugin.
And I don't think your workaround match selected search field (region) with a custom field region and priorize / weight this at the search results to be displayed first, when both fields are the same will work as expected, it will make things complicated.
Here is my suggestion, you might consider custom codes, for example, setup a view with custom search form, with the taxonomy "region" field, but without taxonomy "region" filter.
Then it should be able to display all posts which are matching other user's input.
After user choose one of term in the taxonomy filter, and submit the search form, you can use filter hook "wpv_filter_query_post_process" to trigger a custom PHP function
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
In this function, use custom codes to display those specific posts(match region and priorize / weight) first.
But "wpv_filter_query_post_process" filter hook won't work when pagination is enabled, it works when you display all result in only one pagination.
For your reference.
Hi,
thanks. The wpv_filter_query_post_process seems the right place for this kind of code. We´ve disabled the pagination and created a new custom field region.
How can I match the selected taxonomy region at the query filter with the custom field region?
Can you please show me an example of how I can sort at the hook wpv_filter_query_post_process, because I do not fully understand how I can sort view results at all?
Thanks and regards,
In my opinion, you don't need custom field "region", you just need the taxonomy "region".
And it is a custom codes problem, please provide a test site with the same problem, also point out the problem page URL and view URL, I need a live website to test and debug.