Home › Forums › General Issues › get list of field groups for location rule › Reply To: get list of field groups for location rule
Just updated to do something similar for ACF5. Much easier thanks to the API improvements. Thanks!
/**
* Returns an array of field groups with fields for the passed CPT, where field group ACF location rule of "post_type == CPT" exists.
* - each field group points at an array of its fields, in turn pointed at an array of that field's detailed information:
* - array of info for each field [ ID, key, label, name, type, menu_order, instructions, required, id, class, conditional_logic[array()], etc. ]
*
* @since 1.0.0
*/
function get_acf_field_groups_by_cpt($cpt) {
// need to create cache or transient for this data?
$result = array();
$acf_field_groups = acf_get_field_groups();
foreach($acf_field_groups as $acf_field_group) {
foreach($acf_field_group['location'] as $group_locations) {
foreach($group_locations as $rule) {
if($rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == $cpt) {
$result[] = acf_get_fields( $acf_field_group );
}
}
}
}
return $result;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.