Exclude Entries from Count by Meta or Field Value

Exclude entries from a Limit Submissions feed’s count when a specific entry meta key/value condition is met. The excluded entries still exist, but no longer count toward the configured submission limit.

Instructions

  1. Install this snippet with your preferred method. https://gravitywiz.com/documentation/managing-snippets/

  2. Update the configuration below to match your form, feed, and meta condition.

Code

Filename: gpls-exclude-entries-from-count.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
 * Gravity Perks // GP Limit Submissions // Exclude Entries from Count by Meta or Field Value
 * https://gravitywiz.com/documentation/gravity-forms-limit-submissions/
 *
 * Exclude entries from a Limit Submissions feed's count when a specific entry meta
 * key/value condition is met. The excluded entries still exist, but no longer count
 * toward the configured submission limit.
 *
 * Instructions:
 * 1. Install this snippet with your preferred method.
 *    https://gravitywiz.com/documentation/managing-snippets/
 *
 * 2. Update the configuration below to match your form, feed, and meta condition.
 */
add_action( 'gpls_before_query', function( $rule_test ) {

	// Configuration
	$config = array(
		'form_id'    => 123, // Update '123' to your form ID.
		'feed_id'    => null, // Optional. Set to a feed ID to target a specific GPLS feed.
		'meta_key'   => 'your_meta_key',  // e.g. 'payment_status' or '5' to target field ID 5
		'meta_value' => 'your_meta_value',
	);

	if ( (int) $rule_test->form_id !== (int) $config['form_id'] ) {
		return;
	}

	if ( $config['feed_id'] && (int) $rule_test->feed_id !== (int) $config['feed_id'] ) {
		return;
	}

	global $wpdb;

	$rule_test->where[] = $wpdb->prepare(
		'e.id NOT IN (
			SELECT entry_id FROM ' . $wpdb->prefix . 'gf_entry_meta
			WHERE meta_key = %s AND meta_value = %s
		)',
		$config['meta_key'],
		$config['meta_value']
	);

} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.