0

I'm working on a WordPress shortcode that aggregates data from Gravity Forms entries. I have:

  • Around 50,000 entries on an enrollment form
  • Around 30,000 entries on a feedback form
  • Over 200 trainings
  • I need to calculate averages from a few specific fields across all matching entries for each training and for all trainings as a whole

My initial thought was that I would fetch the entries once and then sort through them for each training and total them up in the process for my global averages.

The issue: Using GFAPI::get_entries() with 'page_size' => 0 or any large range quickly causes:

Allowed memory size of 1073741824 bytes exhausted (tried to allocate 20480 bytes)

When I try chunking with paging, the script then times out (shared hosting — cannot raise limits).

Here’s a simplified version of what I’m doing:

// Now let's get all of the entries $search_criteria = [ 'status' => 'active', 'start_date' => $start, // Which is 2020 'end_date' => $end, // Which is today ]; $enrolled_entries = []; $chunk_size = 500; $offset = 0; do { $paging = [ 'offset' => $offset, 'page_size' => $chunk_size, ]; $entries_chunk = GFAPI::get_entries( $enroll_form_ids, $search_criteria, [], $paging ); if ( is_wp_error( $entries_chunk ) ) { break; } $enrolled_entries = array_merge( $enrolled_entries, $entries_chunk ); $retrieved_count = count( $entries_chunk ); $offset += $chunk_size; } while ( $retrieved_count === $chunk_size ); 

I only need to calculate averages, counts, and basic stats — no need to load every field or the full entry object unless needed.

What I'm looking for:

Is there a better way to efficiently aggregate values across many Gravity Forms entries?

Is there a server-safe way to handle this in chunks, background jobs, or something similar within WordPress?

My full script can be seen here: https://gist.github.com/apos37/f5ef19d3e80aeece138ebab2319fa7ab

Thanks for any help or direction!

2
  • 5
    For something like count, it's probably more efficient to query the database directly via $wpdb::get_var(). Gravity Forms has a gf_entry table that holds entry information. Gravity Forms also has a REST API in which you could dynamically get this data via JS. That being said, this is a 3rd party premium plugin which falls out of scope for this StackExchange. Commented Apr 11 at 16:42
  • 5
    it's unlikely you can process 50k entries even if each entry requires the simplest operation and they can all fit into memory, your going to need to store the result of this operation and then print that in the shortcode, then calculate the data elsewhere, and it'll likely need to happen over multiple requests or cron requests, perhaps a WP CLI command. It's also not reasonable to load all 50k entries into memory at the same time even with unlimited time Commented Apr 11 at 19:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.