Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.