---
title: "Using GravityMath inside a GravityView Custom Content field"
date: 2026-04-23
author: "GravityKit"
link: "https://www.gravitykit.com/docs/gravitymath/using-gravitymath-inside-a-gravityview-custom-content-field/"
---

When using the `[gravitymath]` shortcode inside a GravityView Custom Content field, you may encounter issues with merge tags being processed in the wrong order. This article explains the problem and provides a workaround.

## Understanding the Problem

GravityView processes merge tags in Custom Content fields **before** shortcodes run. This means any merge tags inside your `[gravitymath]` shortcode get replaced by GravityView before GravityMath ever sees them.

## Example Scenario: Sales Team Dashboard

Let's say you have two forms:

**Form A (ID: 10) - Sales Reps**

- Field 1: Sales Rep Name
- Field 2: Region
- Field 3: Email

**Form B (ID: 20) - Orders**

- Field 5: Sales Rep Name (dropdown matching reps from Form A)
- Field 6: Order Total
- Field 7: Order Date

You create a View using Form A to display your sales team. For each rep, you want to show how many orders they have in Form B.

### What you might try (and why it doesn't work)

In a Custom Content field, you add:

```
Orders: [gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]{Order Total:6:count}[/gravitymath]
```

```
Orders: [gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]{Order Total:6:count}[/gravitymath]
```

**Expected result:** Each sales rep shows their order count.

**Actual result:** The count is wrong or empty.

**Why?** GravityView processes `{Order Total:6:count}` first. Since field 6 doesn't exist in Form A (the View's source), it fails. GravityMath never gets to process it.

## The Workaround

We need a way to tell GravityView to leave certain merge tags alone so GravityMath can process them. The workaround below uses an alternate syntax (tildes `~~` instead of curly braces `{}` ) to mark merge tags that should be handled by GravityMath.

### Step 1: Add the helper snippet

Add this code to your site (where to add code):

```
/**
 * Allows using ~Field:ID~ syntax in GravityMath shortcodes.
 * Prevents GravityView from processing these merge tags prematurely.
 * The snippet converts ~~ to {} right before GravityMath runs.
 */
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;
} );
```

```
/**
 * Allows using ~Field:ID~ syntax in GravityMath shortcodes.
 * Prevents GravityView from processing these merge tags prematurely.
 * The snippet converts ~~ to {} right before GravityMath runs.
 */
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;
} );
```

### Step 2: Update your shortcode

Wrap merge tags that reference the **queried form** (Form B) with tildes instead of curly braces:

**Before:**

```
Orders: [gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]{Order Total:6:count}[/gravitymath]
```

```
Orders: [gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]{Order Total:6:count}[/gravitymath]
```

**After:**

```
Orders: [gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]~Order Total:6:count~[/gravitymath]
```

```
Orders: [gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]~Order Total:6:count~[/gravitymath]
```

Now each sales rep's row correctly shows their order count from Form B.

## Which Syntax to Use

| **Syntax** | **Processed by** | **When to use** |
|---|---|---|
| `{Field:ID}` | GravityView (first) | The field is from **the View's form** and you want its current value |
| `~Field:ID~` | GravityMath (after snippet runs) | The field is from **the queried form** and GravityMath should process it |

### In our example:

- `{Sales Rep Name:1}` uses curly braces. This is from Form A (the View's form). We want GravityView to replace it with the current rep's name so it can filter Form B.
- `~Order Total:6:count~` uses tildes. This is field 6 from Form B (the queried form). We need GravityMath to count it after applying the filter.

## More Examples

### Counting filtered entries

Count orders for the current sales rep:

```
[gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]~Order Total:6:count~[/gravitymath]
```

```
[gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]~Order Total:6:count~[/gravitymath]
```

### Summing filtered values

Sum order totals for the current sales rep:

```
[gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]~Order Total:6:sum~[/gravitymath]
```

```
[gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]~Order Total:6:sum~[/gravitymath]
```

### Calculating an average

Average order value for the current sales rep:

```
[gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]~Order Total:6:avg~[/gravitymath]
```

```
[gravitymath scope="form" id="20" filter="filter_5={Sales Rep Name:1}"]~Order Total:6:avg~[/gravitymath]
```

## Troubleshooting

**Still not working?**

- Make sure the snippet is active on your site
- Verify the field IDs match your actual forms
- Check that the filter field (e.g., Sales Rep Name) has matching values in both forms
- Remember: use `{}` for the View's form fields, `~~` for the queried form's fields

If you're still having trouble, [contact our support team](https://www.gravitykit.com/support/) and we'll be happy to help you set this up.

## Related Articles

- [The  Shortcode](https://www.gravitykit.com/docs/gravitymath/math-shortcode/)
- [How to target a specific field value with  filters](https://www.gravitykit.com/docs/gravitymath/how-to-target-a-specific-field-value-with-gvmath-filters/)
- [Using the Custom Content Field](https://www.gravitykit.com/docs/gravityview/getting-started-gravityview/using-the-custom-content-field/)
- [Where to put code samples](https://www.gravitykit.com/docs/gravityview/customizing-your-views/where-to-put-code-samples/)