How I stopped Gravity Forms from performing 1,500 queries per page

For the past two years, I've been working with tpn.health to build an online platform where clinicians earn continuing education credits through live and on-demand classes. When we started out, we used many off-the-shelf plugins, and over time we've written and rewritten tons of custom functionality. One plugin that has been a staple the entire time has been Gravity Forms.

Gravity Forms makes it easy for non-technical staff members to create both simple and complex forms using conditional logic without ever having to write a line of code. It has also allowed our developers to use the plethora of hooks and filters it has built-in to perform all sorts of functions based on the form itself and the data being submitted. It's one of the most useful plugins I've ever used, and I use it on the majority of the sites I've built. So when I received a support ticket from one of the site admins saying that the site was timing out when they tried to edit a post, Gravity Forms was the last thing on my mind as a suspect. But as you probably guessed from the title of the post, I would learn shortly that it was, in fact, our friend Gravity Forms giving us grief.

1,500 Queries?! But how?!?

It's worth noting that TPN uses Gravity Forms a bit more than the average site. Typically a site will have a contact form, maybe a newsletter signup form, an intake form, and a few others here and there. Not TPN. On the day the support ticket came in there were over 500 forms on the site.

Knowing the site had so many forms still didn't raise any red flags. Heck, when I started troubleshooting, Gravity Forms wasn't even on my mind. I thought for sure it was either a hosting issue or possibly some custom code we had written. But nothing obvious was jumping out.

Query Monitor with the big assist

My normal troubleshooting paths weren't turning up anything useful. And, when it no longer looked like it was any of the custom code on the site, I needed to get a sense of what was being run on the page that would be causing the timeouts. That's when I installed Query Monitor.

Query Monitor adds some extra information to the Admin bar to let you know how long the page took to load, the size (in MBs) of the page, and also the total number of database queries performed on the page. This is where things became obvious.

I assume you aren't familiar with how many queries WordPress performs to load up the “Add New Post”. I wasn't either. So, to give you a little context, the query count for that same page on VegasGeek was 429. On TPN, that number was 1,802. Oh my.

Knowing that query count had skyrocketed was only part of the issue. At that point, I didn't know what was causing all the queries. Once again, Query Monitor provided the answer. Not only does it show you a count of the queries, but it also provides a detailed list of the queries being run AND where the query came from. Some come from WordPress core and the rest come from the theme or plugins. As I scrolled through the list, when I reached query #129, this is when I saw Gravity Forms show up. As I continued to scroll it was page after page of Gravity Forms queries.

Query Monitor list of queries

Where did the queries come from?

Once I figured out the issue was related to Gravity Forms, finding what was causing all the queries didn't take very long. In the classic editor, Gravity Forms has a button for quickly adding a form to a post.

If you click the Add Form button, you get a modal that has a dropdown containing every form on your site. In TPN's case, that meant 500+ entries. For each item in that list, Gravity Forms was performing 3 queries. (If you're wondering if switching away from the Classic Editor solved the problem, it did not.)

Gravity Forms filters to the rescue

As I mentioned at the beginning of the post, Gravity Forms has a ton of hooks and filters throughout. Thankfully, they have a very handy filter related to displaying the Add Form button. In the end, the solution was this tiny function:

/**
 * Filter to hide the Gravity Forms "Add Form" button from posts.
 *
 * @return bool
 */
function hide_gf_add_form_button() {
	return false;
}
add_filter( 'gform_display_add_form_button', 'hide_gf_add_form_button' );

In the end, the solution was a simple one. Thankfully. And, it's a great reminder that I should probably be checking out the site with Query Monitor running on a regular basis.

With the filter in place, the query count dropped to 215.

Case closed.

Header photo by Kevin Ku on Unsplash