0

This seems like an extremely simple question, but no matter how I slice it, I need to get data entered by manager and it's employees. Like Manager A added 5 locations, than I want to get those 5 locations and show them in first case. Second, I want to display all records (added by Employee) related to Manager A. Like Employee x, y and z entered records with different locations(added by A) I want to display those records if I select Manager A in dropdown... I can do that with SQL like,

SELECT id FROM _gf_entry where created_by = (ID of Manager A)

->Manager is role in wordpress that I added. than, get all data by SELECT * FROM _gf_entry_meta where entry_id in (SELECT id FROM _gf_entry where created_by = (ID of Manager A))

but I think there should be easy way to do this with gravity form api as it gives manageable results.

2
  • The GFAPI::get_entry_ids() allows you to search for specific created_by IDs; see the Field Filters under the Search Arguments section for an example.
    – Pat J
    Commented Dec 23, 2023 at 18:24
  • That won't help the OP get the entry data from the query, they didn't state they wanted only ID in return.
    – Kyzer
    Commented Mar 19 at 4:03

2 Answers 2

0

You should be able to achieve this using the Gravity Forms API in WordPress to filter entries created by a specific user (Manager A) and retrieve associated data entries.

  1. Get Manager A's User ID:

    First, you need to get the User ID of Manager A. You can do this programmatically in WordPress. Let's assume you have Manager A's User ID stored in a variable called $manager_id.

  2. Query Gravity Forms Entries:

    You can use the GFAPI::get_entries function to retrieve entries that meet your criteria. In your case, you want to filter entries created by Manager A. Here's an example of how to do this:

    $entries = GFAPI::get_entries(
        array(
            'form_id' => 1, // Replace with your form ID
            'created_by' => $manager_id,
        )
    );
    

    Replace 1 with the actual ID of your Gravity Form.

  3. Retrieve Associated Data Entries:

    Now that you have the entries created by Manager A, you can loop through them and retrieve associated data entries using the _gf_entry_meta table. Here's how you can do it:

    $all_related_entries = array();
    
    foreach ($entries as $entry) {
        $entry_id = $entry['id'];
    
        // Query the _gf_entry_meta table to get associated data
        $data_entries = $wpdb->get_results(
            $wpdb->prepare(
                "SELECT * FROM {$wpdb->prefix}gf_entry_meta WHERE entry_id = %d",
                $entry_id
            ),
            ARRAY_A
        );
    
        // Add data entries to the result array
        $all_related_entries = array_merge($all_related_entries, $data_entries);
    }
    

    Make sure you have global access to the $wpdb object to run database queries.

  4. Display the Results:

    Now you have the entries and associated data entries in the $all_related_entries array. You can loop through this array and display the results as needed.

    foreach ($all_related_entries as $data_entry) {
        // Display the data as per your requirements
        // For example: echo $data_entry['your_field_name'];
    }
    

    Replace 'your_field_name' with the actual field name you want to display.

  5. Dropdown Selection:

    If you want to dynamically select Manager A from a dropdown, you can use a dropdown field in your Gravity Form. Then, you can modify the $manager_id variable based on the selected option.

This code should help you retrieve and display entries and associated data entries created by Manager A using the Gravity Forms API. Make sure to replace placeholders with actual values, and adjust the code to fit your specific form and field names. Hope this helps!

1
  • I am still getting all entries. It should fetch only entries with created by Id..
    – Dharmik
    Commented Dec 28, 2023 at 17:21
0

@dharmik I think what you are wanting is something like this:

  $user_id = get_current_user_id();
  $search_criteria = array(
    'field_filters' => array( //which fields to search
      array(
        'key' => 'created_by', 'value' => $user_id, // Current logged in user
        // ... Add more keys/values if needed
      )
    )
  );
  $sorting         = array();
  $paging          = array( 'offset' => 0, 'page_size' => 999 );
  $subform_entries = GFAPI::get_entries( 20, $search_criteria, $sorting, $paging );

The get_entries is looking for field_filters. In you case it's created_by

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.