Skip to content

Instantly share code, notes, and snippets.

@craigedmonds
Created August 27, 2019 10:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigedmonds/e750c6734b8bec47ca04b4d209b5f5f2 to your computer and use it in GitHub Desktop.
Save craigedmonds/e750c6734b8bec47ca04b4d209b5f5f2 to your computer and use it in GitHub Desktop.
Get a list of field names and values from an ACF Group
<?php
########################
// GET FIELD NAMES AND VALUES FROM ACF GROUP
//
// created by craig@jucra.com on 27/8/2019
//
// This script will do the following:
// 1. Loop through the fields in a specific ACF Group
// 2. Create an array called $array_of_field_names
// so you can easily print out a list of the field names
// 3. Create an array called $array_of_field_values
// so that you can print out a list of the names and values
// 4. Creates a variable for each field so you can simply copy and
// paste the variable directly into your page without having to
// write out the query
########################
$acf_group_id = 11;
$acf_fields = acf_get_fields($acf_group_id);
$count = 0;
foreach($acf_fields as $acf_field) {
//get the field name
$field_name = $acf_fields[$count]["name"];
//create a dymanic variable and grab the value of the field
${"$field_name"} = get_field($field_name);
//create a nice list of variable names during development
$array_of_field_names[] = $field_name;
//BONUS: create an array in case you want to print everything out
$array_of_field_values[] = array(
'field_name' => $field_name,
'value' => ${"$field_name"},
);
$count = $count + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment