Skip Navigation

[Resolved] ow to make Views sortable by clicking on table headings

This support ticket is created 4 years, 7 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 11 replies, has 2 voices.

Last updated by Luo Yang 4 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1601135

I know it's possible to do this because I've seen it implemented.

How can I set up a view such that users can sort the table view output in ascending or descending order?

I assume it's got something to do with wrapping the table heading in the "wpv-heading" shortcode like this:

[wpv-heading name="post-title"][wpml-string context="wpv-views"]Listing Description[/wpml-string][/wpv-heading]

But can this work for custom fields as well as post titles?

#1602109

Hello,

It is possible to setup [wpv-heading] shortcode to order the result by custom field, see our document:
https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-heading

You can use Views GUI, enable option "Make the table sortable by columns", and generate those shortcodes:
https://toolset.com/documentation/user-guides/views/view-layouts-101/#table

#1602301

If the loop item in turn uses a content template for a custom field, is the output of that template still sortable?
e.g.

VIEW "Vehicles"
<!-- wpv-loop-start -->
<table>
<tr>
<th>Category</th>
<th>Price</th>
</tr>
<wpv-loop wrap="13" pad="true">

[wpv-item index=1]
[wpv-post-body view_template="loop-item-in-table-view-format"]
[wpv-item index=other]
[wpv-post-body view_template="loop-item-in-table-view-format"]
[wpv-item index=13]
[wpv-post-body view_template="loop-item-in-table-view-format"]
[wpv-item index=pad]
[wpv-item index=pad-last]
</wpv-loop>
</table>
<!-- wpv-loop-end -->

CONTENT TEMPLATE "loop-item-in-table-view-format"
<tr>
<td>[wpv-post-taxonomy type="listing_cat" format="name"]</td>
<td>[wpv-post-body view_template="Price"]</td>
</tr>

CONTENT TEMPLATE "Price"
[wpv-conditional if="( $(wpcf-price) gt '0' )"]
[format-currency num='[types field="price" output="raw"][/types]'] JPY
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-price) eq '0' )"]
[wpml-string context="wpv-views"]Free![/wpml-string]
[/wpv-conditional]

#1602363

Yes, [wpv-heading] shortcode will be able to sort view's result by custom fields, it does not care content templates insider loop.
And Views provides another way to sort the result with dropdown menus, see our document:
https://toolset.com/documentation/user-guides/views/allowing-visitors-to-sort-the-front-end-results/
Allowing visitors to sort the front-end results

#1603457

OK. Thanks. I am aware that it's possible to insert "Sort Filters" into the View Filter.
The screenshot you linked to looks different to mine though:
hidden link

I'm using the current Views version.
Does the documentation need updating?

#1603469

Are we talking about the section "Search and Pagination" of above document?
If it is, please follow the document I mentioned above:

You can enable them by clicking the Screen Options button at the top-right corner and selecting the checkboxes for Custom Search Settings and Search and Pagination.

https://toolset.com/documentation/user-guides/views/allowing-visitors-to-sort-the-front-end-results/#how-to-enable-front-end-sorting

section "How to Enable front-end sorting"

#1603475

OK! Now I get it.
The docs are referring to the "Screen Options" at top right.
Thanks.

Anyway, that's a bit of a side track because I would REALLY like to make the table output sortable
via TABLE COLUMN HEADINGS.

When I use the [wpv-heading] shortcode it makes the table heading "clickable" but the sorting doesnt work properly.
I don't know what field it sorts on, but its not the desired one.

I want to check, what should I be using in the "name" parameter.
is it:

<th>[wpv-heading name="fuel-type"]Fuel Type[/wpv-heading]</th>
or
<th>[wpv-heading name="wpv-fuel-type"]Fuel Type[/wpv-heading]</th>
or
<th>[wpv-heading name="wpcf-fuel-type"]Fuel Type[/wpv-heading]</th>

I can't find documentation around that.

#1603499

As I mentioned above, you can use Views GUI, enable option "Make the table sortable by columns", and generate those shortcodes:
https://toolset.com/forums/topic/ow-to-make-views-sortable-by-clicking-on-table-headings/#post-1602109

If you need more assistance for it, please provide a test site with the same problem, also point out the problem page URL and view URL, thanks

#1603509

I understand if its a NEW view I can use that GUI to make the table columns clickable.
But what if it's an EXISTING view?

#1603511

I assume we are talking about custom field "Price" created with Toolset Types plugin, if it is, you can try below shortcode:
[wpv-heading name="types-field-price"]Price[/wpv-heading]
See the document I mentioned above:
https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-heading

For types field, the field name must be prefixed with types-field. So, to sort by the types field ‘property-name’, name=”types-field-property-name”.

#1603567

OK. Brilliant.
[wpv-heading name="types-field-price"]Price[/wpv-heading]
That was the missing piece of the puzzle.

Last question. What if the sort column ISNT a custom field but instead is:
a) Taxonomy field (category)
b) A parent relationship field e.g. "model" of car
c) A grandparent relationship field e.g. "make" of car

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/what-if-the-sort-column-isnt-a-custom-field/

#1603587

I assume the original question of this thread is resolved, according to our support policy, we prefer to one question one ticket, for other new questions, please check the new thread here:
https://toolset.com/forums/topic/what-if-the-sort-column-isnt-a-custom-field/