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?
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
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]
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
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?
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"
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.
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
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?
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”.
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
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/