Using GravityMath inside a GravityView Custom Content field
A [gravitymath] shortcode in a GravityView Custom Content field can calculate over the entries of another form, such as counting each teacher’s lessons in a View of teachers. Before the shortcode runs, GravityView fills in the merge tags in the field with the values of the row’s entry, and a merge tag meant for the other form can be filled in with them.
When a merge tag is filled in too early #
GravityView fills in a merge tag when the View’s form has a field with its ID. A merge tag for a field ID that the View’s form doesn’t have is left for GravityMath, which calculates it over the other form. When the View’s form also has a field with that ID, GravityView puts the row’s value of that field in its place. GravityMath then has nothing to count, and the row shows nothing, with no error.
Example: counting lessons per teacher #
A music school has two forms:
- Teachers (form 10), the form the View uses: Name (field 1), Instrument (field 2), and Email (field 3).
- Lessons (form 20): Lesson (field 1), Date (field 2), Teacher (field 5, the teacher’s name), and Hours (field 6).
A View of the Teachers form gets a Custom Content field that counts each teacher’s lessons:
[gravitymath scope="form" id="20" filter="filter_5={Name:1}"]{Lesson:1:count}[/gravitymath]{Name:1} belongs to the Teachers form and is meant to be filled in: it puts the row’s teacher in the filter. {Lesson:1:count} belongs to the Lessons form, but the Teachers form also has a field 1, so GravityView fills it in with the teacher’s name, and the column stays empty.
Counting a field whose ID the Teachers form doesn’t have, such as {Teacher:5:count}, works until someone adds a field 5 to the Teachers form.
Keeping merge tags for GravityMath with tildes #
The snippet below lets you write the other form’s merge tags between tildes, such as ~Lesson:1:count~. GravityView leaves those alone, and the snippet turns them back into merge tags right before GravityMath calculates.
Step 1: Add the snippet
/**
* Turns ~Label:ID:modifier~ into {Label:ID:modifier} right before GravityMath
* calculates, so GravityView leaves those merge tags alone in Custom Content fields.
*
* @param string $formula The formula of a [gravitymath] shortcode.
*
* @return string The formula with the tilde merge tags turned into merge tags.
*/
add_filter( 'gravityview/math/shortcode/before', function ( $formula ) {
preg_match_all( '/~[^~]*?:(\d+(\.\d+)?|[a-z_]+)(:(.*?))?~/mi', $formula, $merge_tags, PREG_SET_ORDER );
foreach ( $merge_tags as $merge_tag ) {
$updated_merge_tag = str_replace( '~', '', $merge_tag[0] );
$updated_merge_tag = sprintf( '{%s}', $updated_merge_tag );
$formula = str_replace( $merge_tag[0], $updated_merge_tag, $formula );
}
return $formula;
} );Note: Not sure where to add the code? See Where to put code samples.
Step 2: Write the merge tags of the other form with tildes
In the Custom Content field, replace the curly braces of the Lessons form’s merge tag with tildes:
[gravitymath scope="form" id="20" filter="filter_5={Name:1}"]~Lesson:1:count~[/gravitymath]Each teacher’s row now shows their number of lessons. The Hours and Average columns use the examples further down.

Which syntax to use #
| Syntax | Filled in by | Use it for |
|---|---|---|
{Name:1} | GravityView, with the row’s entry | A field of the View’s form, such as the value to filter by |
~Lesson:1:count~ | GravityMath, after the snippet | A field of the form in the shortcode’s id parameter, the form being calculated |
More examples #
Total hours for the row’s teacher:
[gravitymath scope="form" id="20" filter="filter_5={Name:1}"]~Hours:6:sum~[/gravitymath]Average lesson length, with two decimals:
[gravitymath scope="form" id="20" filter="filter_5={Name:1}" decimals="2"]~Hours:6:avg~[/gravitymath]Filtering by the entry ID of the row #
When the other form stores the ID of the row’s entry, such as a Teacher ID field (field 7) that holds the teacher’s entry ID, filter on {entry_id}. Put the tilde merge tag in the formula parameter, ahead of the filter:
[gravitymath scope="form" id="20" formula="~Lesson:1:count~" filter="filter_7={entry_id}" /] lessons for {Name:1}With the tilde merge tag after the filter, between the shortcode tags, any merge tag later in the same Custom Content field breaks the row. The row then shows a cut-off shortcode, such as [gravitymath scope="form" id="20" filter="filter_7=Ana Lopez. Gravity Forms reads everything from {entry_id} to that later merge tag as a single merge tag.
Troubleshooting #
| What you see | What to do |
|---|---|
| The column is empty | A merge tag for the other form is written with curly braces, and the View’s form has a field with the same ID. Write it with tildes, and check that the snippet is active. |
| The column shows 0 | The filter matches no entries. The field you filter on in the other form has to hold the value the merge tag fills in, such as the teacher’s name. |
| The row shows part of a shortcode | A tilde merge tag follows a filter on {entry_id}, and another merge tag comes later in the field. Move the formula into the formula parameter, as in Filtering by the entry ID of the row. |