How to Calculate the Sum of a Field in Gravity Forms

Using the [gravitymath] shortcode you can calculate the sum of a field for all entries in a form. 

Prefer to Watch the Video?

We need two things to find the sum of a field for all form entries:

  1. The Form ID
  2. The Merge Tag of the field we want to sum

The shortcode will look like this:

[gravitymath scope="form" id="4"] {merg tag:2} [/gravitymath]
		

scope="form" tells the shortcode that we’re working with data from a form (as opposed to a View). The id value refers to the ID of your form in Gravity Forms and the merge tag should be the merge tag of the field you want to sum. Merge tags are always wrapped in curly brackets.

Note: The default behavior for [gravitymath] is to output a sum. If you want to output a count instead, you can use the :count operator, like this: {merge_tag:2:count}.

Finding Your Form ID: 

Hover over Forms in your WordPress admin menu and click on Forms, you’ll see the form ID in the first column on the right.

The Forms page in Gravity Forms with the form IDs showing in the ID column on the right

Finding the Field’s Merge Tag

Here's an easy way to find the merge tag for any field in your form.

From the form editor, hover over Settings at the top and click on Notifications.

The Notifications link that appears when you hover over Settings from the form editor in Gravity Forms

Now, click Add New to create a new notification (we’re not going to save it, we’re only using this as a way to get the merge tag!).

The Gravity Forms Notifications screen with an arrow pointing to the Add New button

Scroll down to the text editor and click on the merge tag button on the right. 

An arrow pointing to the merge tag button

Now, select the name of the field you want to sum from the list.

An arrow pointing to the Number of Attendees field in the merge tag list

You'll see the field merge tag appear in the text editor. Copy it to add to your shortcode.

The Number of Attendees merge tag inside the text editor

Displaying the Sum of a Field on Your Website

You can add the [gravitymath] shortcode anywhere on your website! 

As you can see, here we’ve added it to a new page.

The Gutenberg page editor showing the gv_math shortcode

And on the front end, the shortcode is replaced with the sum of all values in the field.

The page on the front end showing a number in place of the shortcode

Adding the Shortcode to a View

You can add the [gravitymath] shortcode to a View by pasting it inside a Custom Content field or widget.

To do this, go to your View editor, click the Add Widget button and select Custom Content.

The GravityView Add Widget button with an arrow pointing to the Custom Content field

Next, open the widget settings by clicking on the gear icon. Now, paste the shortcode inside the text editor along with any other text/HTML you want to display.

The Custom Content field text editor showing the gv_math shortcode

As you can see, the shortcode is replaced on the front end.

A View on the front end with the shortcode replace by the number 6

To learn more about [gravitymath] and how to perform advanced calculations, read our ultimate guide to Gravity Forms calculations.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us