Skip to content

Instantly share code, notes, and snippets.

@stefanmm
Created July 24, 2022 22:51
Show Gist options
  • Save stefanmm/d82555d912ffb74dace276eec5adbbf3 to your computer and use it in GitHub Desktop.
Save stefanmm/d82555d912ffb74dace276eec5adbbf3 to your computer and use it in GitHub Desktop.
Get Gravity Forms entries by field value
<?php
/* https://docs.gravityforms.com/searching-and-getting-entries-with-the-gfapi/
* $form_id: ID of the GF form. Default is 0 which means "All" (int)
* $field_id: ID of the field inside the GF $form_id (string)
* $field_val: value you want to search (string)
* $operator: =, IS, CONTAINS, IS NOT, ISNOT, <>, LIKE, NOT IN, NOTIN, IN (string)
* $limit: number of results to return (int)
*/
function gf_get_entries_by_field( $form_id = 0, $field_id = "", $field_val = "", $operator = "=", $limit = 10 ){
if ( !class_exists('GFAPI') ) { return; } // bail early if GF class doesn't exist
$search_criteria['field_filters'] = array( 'key' => $field_id, 'operator' => $operator, 'value' => $field_val );
$paging = array( 'offset' => 0, 'page_size' => $limit );
return GFAPI::get_entries( $form_id, $search_criteria, array(), $paging );
}
// Usage
$results = gf_get_entries_by_field( 4, "full_name", "John Doe", "IS", 5 );
if( $results && !empty($results) ){
foreach($results as $entry){
echo $entry["4.3"]; // Value of the field 4 (3 is a sub-field)
echo $entry["6"]; // Value of the field 6
// etc..
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment