This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
This hook allows you to manipulate how a view is ordered.
Usage
add_filter('frm_view_order', 'change_view_order', 10, 2); function change_view_order($query, $args)
Parameters
- $query (array)
- $args (array)
Examples
Order by Month
This example will allow you to order dates in a view by their month, ignoring the year in the date field.
add_filter('frm_view_order', 'change_view_order', 10, 2);
function change_view_order($query, $args){
if ( $args['display']->ID == 2412 ) { //replace 2412 with you Field ID
$query['order'] = 'ORDER BY MONTH(it.created_at) ASC';
}
return $query;
}
Create a Leaderboard
Use this code example to create a leaderboard or similar type of View. For example, you could show people who are participating in a contest, ranked from highest number of points to lowest. Or you could show salespeople ranked from highest sales to lowest.
- The form for this View would have a field to identify the person or entity and a field for an amount
- The View would have a filter that the person or entity field is unique. So, with a leaderboard, each person's name would be listed once.
- This code will order the entries by the total of the amount field. So, with a leaderboard, the person with the most points would be first and the person with the fewest points would be at the bottom.
add_filter( 'frm_view_order', 'frm_order_by_sum', 10, 2 );
function frm_order_by_sum( $query, $args ) {
global $wpdb;
if ( $args['display']->ID != 1478 ) { // Change 1478 to the id of your View.
return $query;
}
$total_field_id = 4235; // Change 4235 to the id of the field whose sum you want to use for ordering
$unique_field_id = 4236; // Change 4236 to the id of the unique field.
// Create common table expression with totals
$select = "with em0 as (SELECT im2.meta_value, im1.item_id as item_id_column, SUM(im1.meta_value) OVER (PARTITION BY im2.meta_value) as 'total_points'";
$select .= "FROM " . $wpdb->prefix. "frm_item_metas im1 JOIN " . $wpdb->prefix . "frm_item_metas im2 ON im1.item_id = im2.item_id ";
$select .= "WHERE im1.field_id = ". $total_field_id . " AND im2.field_id = " . $unique_field_id . ") ";
$select .= "SELECT it.id from " . $wpdb->prefix . "frm_items it LEFT JOIN em0 ON em0.item_id_column=it.id ";
$query['select'] = $select;
$query['order'] = "ORDER BY total_points DESC"; // Change DESC to ASC if you want the lowest value first.
return $query;
}
- Change 1478 to the id of your View.
- Change 4235 to the id of the field whose sum you want to use to order the View, like the field that stores the number of points a player has earned.
- Change 4236 to the field is filtered to show unique values, like the player's name.
- This code example takes care of the sorting by total points. To show the total points in the View, you can havea statistic using the frm-stats shortcode. The stat could look like this: [frm-stats id=4235 type=total 4236="[4236]"], where 4235 is the id of the points field and 4236 is the field that holds the player's name.