Formidable Forms

Formidable Forms

  • Features
  • Pricing
  • Resources
    • Docs & Support
    • Blog
    • Community
  • Solutions
    • Web Applications
    • Calculators
    • Surveys
    • Directories
    • Payments
    • Contact forms
  • Login
  • Get Formidable Forms

frm_view_order

Last Updated: April 30, 2018

Knowledge Base → Extend Formidable Forms → Formidable Hooks - for Developers → Display Data → frm_view_order
Heads up!
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.

Formidable Forms is the best WordPress Form Builder plugin. Get it for free!

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.

  1. The form for this View would have a field to identify the person or entity and a field for an amount
  2. 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.
  3. 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;
}
  1. Change 1478 to the id of your View.
  2. 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.
  3. Change 4236 to the field is filtered to show unique values, like the player's name.
  4. 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.
  • Usage
  • Parameters
  • Examples
    • Order by Month
    • Create a Leaderboard
Categories
×

Categories

  • Installation & Getting Started
  • Account Management
  • Forms
  • Entries
  • Views
  • Styles
  • Importing & Exporting
  • Add-Ons
  • Extend Formidable Forms

Using WordPress and want to get Formidable Forms for free?

Get Formidable Forms Lite Now

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

Take on bigger projects Right Now

Get the tools you need to revolutionize your workflow and architect a masterpiece. Build the most advanced WordPress forms and actually use the data you collect in meaningful ways.

Get the most advanced WordPress form plugin and the only form builder with integrated Views.

Get Formidable Forms Now

Resources

  • Community
  • Affiliates
  • Contact
  • Free Online Form Builder

Top Features

  • Application Builder
  • Calculator Forms
  • Surveys & Polls
  • Quiz Maker
  • Form Templates
  • Application Templates
  • Directories
  • Donation Plugin

Company

  • About Us
  • Giving Back
  • Careers
  • Newsletter
  • WP Tasty
  • Nutrifox

Copyright © 2023 Strategy11, LLC. Formidable FormsĀ® is a registered trademark Strategy11, LLC.
Privacy Policy | Terms of Service | Sitemap

Join 300,000+ using Formidable Forms to create form-focused solutions fast. Get Started See User Reviews