Skip Navigation

[Resolved] Filter View – sorting results

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

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 2 voices.

Last updated by Beda 4 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#1510135

Hello,

I would like to insert a sorting option in my filter view.
I inserted the following code :

<div class="form-group">[wpv-sort-order type="select" options="desc,asc" label_asc_for_field-ratings_average="Les moins bien notées" label_desc_for_field-ratings_average="Les mieux notées"]
</div>

The filter works perfectly and only shows the posts with at least 1 rating in the ascending or descending order.
My problem is that, my view is sorted by "Date of Post" but sorting code, my results are automatically sorted by an ascending ratings_average value of post.

I basically would like the filter to be active only when a user select a dropdown option "sort result by ascending order" or "sort result by descending order". The filter should not be active if no options are selected.

Thank you inadvance for your help.
Best regards,
Damien,

#1510155

That is already a sorting option.
I am not sure what you mean with the "Filter that should be active only if a user sorts by ascending order"

If you mean to show a Custom Front End filter to filter custom fields, but only when certain conditions are met, you can use HTML conditions, wrap the section you want to show/hide and craft a condition that listens to the data you want to display content based on.

In your case that could be the URL parameter that the sorting option adds in the URL when used.
To access URL parameters you can use the shortcode [wpv-search-term] where you then will receive a response about the specified URL parameter.
This then allows you to craft an HTML condition where you show certain content only if the specified search parameter matches your desired value (desc or asc in this case)

But this only helps to show/hide content or HTML in general, not to fully disable or enable the actual Query filter.

Would this help?
You can find more details on HTML conditionals here
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/

The wpv-search-term is explained here
https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-search-term

#1512711

Hello Beda,

Thank you for your answer.
Sorry, I might have not been clear with my descritption.

No, in fact, by "Filter that should be active only if a user sorts by ascending order", I mean that when the user arrive on the page with the filter view, the filter " already has the value "ascending order" active.

I would like the "ascending descending filter" to only be active once the user click on it. I don't want the filter to be active when the user load the url, the filter has to remain inactive until triggered by the user.

Just like on Booking.com, when a user arrive on a sorting page, the results are not organized in a score ascending order but the user can activate a filter that would sort the result in a score ascending or descending order.

Basically, the filter allow the user to sort by score but the results are not filtered by score unless the user wants to use it.
This is the url of the filter view : hidden link

Hope this make sense,
Thank you in advance Beda,

#1514343

One ordering will always be active and its the first option in the Front End ordering or, in other words, the option saved as ordering in the backend in the OrderBy settings of the View.
That setting cannot be overwritten.

Hence, ordering will always (and does) follow that first option, which anyway cannot be removed as it's the actual OrderBy setting of the very view.
Even on Booking.com, they'll respect some (maybe to us invisble) ordering.
Every query does, it's the nature of a query. You could set the backend order to first option a random order, and make sure the View orderby is also random (or any other option you want to be used as "default", because you'll have to use one.)

Does this clarify things?

#1516711

Hello Breda,

Thank you very much for your answer.
I understand, my view actually has a "OrderBy" "date of the post".
It means that when the view is displayed, the latest post is the first shown post.
In other words, the most recent post is the first in my result.
And that's perfect.

But I would like to allow the user to sort the view differently if they click on the "Sorting Option - Sort by rate" :
"Show the best rated result first (Descending)"
"Show the worst rated result first (Ascending)".

My "Sorting option - sort by rate" filter also works perfectly and I am very happy with it.

My Problem is that the "Sorting option - sort by rate" DIRECTLY takes the place of the Inital OrderBy Date when the page load. I would like this "Sorting option - sort by date" option to ONLY be active if the user decide to sort the result by rate.

Basically, I would like to have an OrderBy "Date of the post" active until the user select the filter "Sort by rate" (that would then take over the sort by date of the post query).

IIs that possible with toolset ?
Thank you

#1517853

Well, it should be taking the first option saved, which classically is the Orderby option taken from the views settings, but you could change that in the very Front End orderby when you add order by and sort options to the front end.

Can I see this in the backend?
I suspect it just takes to switch the first default order option in the controls, but to verify this I'd need to see the View where you build it.
Can you link me to it, and add access so I can see it?
I'll not make any changes to the existing View, but if considered helpful, I can make a copy of that View and amend it so to show how to make it work.

Would that help?