Add Default Filter to Entries Query

This snippet adds a default filter to the Entries Query for all Entry Blocks. While this has many uses, this snippet’s primary intent is to ensure that only users can only see entries that belong to their own group. In this scenario, each entry has a field indicating its “Group ID” and each user has a user meta value indicating to which group they belong.

Code

Filename: gpeb-entries-query-default-filter.php

<?php
/**
 * Gravity Perks // Entry Blocks // Add Default Filter to Entries Query
 * https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
 *
 * This snippet adds a default filter to the Entries Query for all Entry Blocks. While this has many uses, this snippet's
 * primary intent is to ensure that only users can only see entries that belong to their own group. In this scenario,
 * each entry has a field indicating its "Group ID" and each user has a user meta value indicating to which group they
 * belong.
 */
add_filter( 'gpeb_entries_query', function( $filters, $form_id, $block_context ) {

	// Update "123" to your form ID.
	if ( $form_id !== 123 ) {
		return $filters;
	}

	$raw_filter = array(
		// Update "1" to the ID of the field in which the group to which an entry belongs is stored.
		'property' => '1',
		'operator' => 'is',
		// Update "groupid" to the meta key of the user meta value indicating to which group the user belongs.
		'value'    => 'gf_custom:{user:groupid}',
	);

	static $_running;

	if ( $_running ) {
		return $filters;
	} else {
		$_running = true;
	}

	$queryer          = GP_Entry_Blocks\GF_Queryer::attach( $block_context );
	$processed_filter = $queryer->process_filter( array(), array(
		'filter_value'       => apply_filters( 'gpeb_filter_value', $raw_filter['value'], $raw_filter, $queryer ),
		'filter'             => $raw_filter,
		'filter_group'       => array(),
		'filter_group_index' => 0,
		'form_id'            => $form_id,
		'property'           => null,
		'property_id'        => $raw_filter['property'],
	) )[0][0];

	if ( empty( $filters ) ) {
		$filters[] = array();
	}

	// Add our filter as an "AND" condition for each filter group to ensure that it cannot be unintentionally bypassed.
	foreach ( $filters as &$group ) {
		$group[] = $processed_filter;
	}

	$_running = false;

	return $filters;
}, 10, 3 );

Comments

  1. Ana
    Ana August 14, 2024 at 8:42 am

    Is it possible to query a user’s entries from multiple forms? For example, UserA has entries in Forms 1, 2, and 3. I would like to display all of UserA’s entries in a single table or list view. The columns should include: Form Title, Date Created, Delete, and Edit.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff August 14, 2024 at 9:15 am

      Hi Ana,

      The GP Entry Blocks Perk currently doesn’t support querying multiple forms to get entries matching a specific filter. You can submit this as a feature request via our support form.

      Best,

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.