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.

I tried this solution and pre_get_posts will not work because it can’t detect a query and post_type at this url.
/wp-admin/admin.php?page=gf_entries
There needs to be a post_type url parameter with pre_get_posts to work.
Any other ideas?

Another way to accomplish this is by using the Gravity Forms REST API. The Gravity Forms REST API provides a way to retrieve and update form entries programmatically, and you can use it to retrieve only the entries with the franchise_location field set to “Miami.”

Here’s an example of how the Gravity Forms REST API can be used to get the entries:

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

$api_key = 'your_api_key';
$api_url = 'your_site_url/wp-json/gravityforms/v2/entries';

$args = array(
    'method' => 'GET',
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( $api_key ),
        'Content-Type' => 'application/json'
    ),
    'body' => json_encode(array(
        'form_id' => $form_id,
        'field_filters' => array(
            array(
                'key' => 'franchise_location',
                'value' => $franchise_location
            )
        )
    ))
);

$response = wp_remote_request( $api_url, $args );

if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
    $entries = json_decode( wp_remote_retrieve_body( $response ), true );
}

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

This way, you can filter the entries using the Gravity Forms REST API and display them on a custom admin page without modifying the query using pre_get_posts.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.