Skip Navigation

[Resolved] Order view results by selected search parameter

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

Problem:

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?

Solution:

There isn't existed workaround for it, you might consider custom codes, for example:

https://toolset.com/forums/topic/order-view-results-by-selected-search-parameter/#post-1125213

Relevant Documentation:

This support ticket is created 5 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by rainerS 5 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1124639

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,

#1125126

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/

#1125127

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,

#1125213

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.

#1125261

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,

#1125860

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.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.