Learn how to show form submissions on a page, and sort a view across pages. This allows users to sort by values on the page, including values that are shown on a different page when using pagination. If your view is a table without pagination, read how to make a sortable table. Note: This isn't set up to work with Dynamic fields.
Create the view
If you haven't created a view yet, start there.
Add links to sort
Once your view is complete, add links to sort the data. For example, you may have a table header that looks like this:
<th>Date</th>
This label needs to be switched to a link.
<th><a href="?frmsort=25&frmdir=[if get param='frmdir' equals='asc']desc[/if get][if get param='frmdir' not_equal='asc']asc[/if get]">Date</a></th>
In this link, replace 25 with the id of the field to order by when 'Date' is clicked. Now, each time you click this link it will switch between frmsort=25&frmdir=asc and frmsort=25&frmdir=desc to change the direction of the sorting.
Add as many different sorting links that you would like. Be sure to change the field id in each link.
Sort the view
All that is left is to tell your view how to use the frmsort and frmdir values. To do this, we'll need a custom shortcode that uses the PHP alternative to insert a view.
add_shortcode( 'sort-frm-view', 'sort_frm_view_shortcode' ); function sort_frm_view_shortcode( $atts ) { if ( isset( $_GET['frmsort'] ) ) { $atts['order_by'] = sanitize_text_field( $_GET['frmsort'] ); } if ( isset( $_GET['frmdir'] ) ) { $atts['order'] = sanitize_text_field( $_GET['frmdir'] ); } return FrmViewsDisplaysController::get_shortcode( $atts ); }
Insert the view on a page
Since you have a custom shortcode, the view will be inserted on the page a bit differently than normal. Now you will use 'sort-frm-view' in place of 'display-frm-data' in your shortcodes. If your original view shortcode looked like this:
[display-frm-data id=150 filter=limited limit=10]
the new shortcode will be:
[sort-frm-view id=150 filter=limited limit=10]
Dynamically sort the content
Now the order of entries in the view will change each time a link is clicked. This covers any number of fields that you would like to sort.