Custom view of form entries based on a form entry's field value

For example:

There is a business with multiple franchises.

The website has a custom post type called Franchise and a user role called Franchisee.

A Franchisee miami_manager is assigned to Franchise miami.

Rather than creating multiple forms for different franchises the same form Estimate is used for all franchises.

A hidden field franchise_location is dynamically populated to that Estimate form based on the url it’s submitted from. This will help distinguish the different form entries.

I’ve programmed the above already but how would I do the rest of this.

Question:

When miami_manager logs into the dashboard he can only see form entries that contain the field franchise_location with a value of miami.

Possible way of doing this:

Using pre_get_posts and posts_where. Is this possible or is there a better way of doing this?

You can use the Gravity Forms API to filter the form entries based on the value of the franchise_location field. You can use the GFAPI::get_entries() function to retrieve the form entries and then the GFAPI::count_entries() function to filter the entries based on the value of the franchise_location field.

Here’s an example of how you can filter the form entries based on the value of the franchise_location field:

$form_id = 1; // ID of the Estimate form
$franchise_location = 'miami'; // Value of the franchise_location field

$search_criteria = array(
    'field_filters' => array(
        array(
            'key' => 'franchise_location',
            'value' => $franchise_location
        )
    )
);

$entries = GFAPI::get_entries($form_id, $search_criteria);

You can then use this variable $entries to display the form entries on the dashboard, where only the form entries that contain the franchise_location with a value of ‘miami’ will be displayed.

Alternatively, you can use the GFAPI::count_entries() function to retrieve the number of form entries that match your search criteria and then use that number to display the form entries on the dashboard.

$entry_count = GFAPI::count_entries($form_id, $search_criteria);

This is a possible way to do this using the Gravity Forms API, but you can also use other methods like pre_get_posts and posts_where. It depends on your specific use case and requirements.

Hi @faisalahammad
That is great to know. I’ll look into the docs for GPAPI::get_entries.
It looks like I would have to build a custom admin page that displays the entries as well.

If I were to use pre get_posts I would need to know the post_type of the entries. What is the post_type for gravity forms entries?

The post type for Gravity Forms entries is “gf_entry.” So you can use the pre_get_posts action to filter the entries based on the value of the franchise_location field.

Here’s an example of how you can use pre_get_posts to filter the entries based on the value of the franchise_location field:

function custom_gravity_forms_query( $query ) {
    if ( !is_admin() || !$query->is_main_query() ) return;
    if( $query->get('post_type') == 'gf_entry') {
        $meta_query = array(
            'key' => 'franchise_location',
            'value' => 'miami',
            'compare' => '='
        );
        $query->set( 'meta_query', array($meta_query) );
    }
}

add_action( 'pre_get_posts', 'custom_gravity_forms_query' );

This code will filter the entries with the post_type ‘gf_entry’ and franchise_location equal to ‘miami’ and only show the entries that match the criteria for the user logged in.

You’ll still need to build a custom admin page displaying filtered entries. However, you can use the filtered entries to display them on that page.