Last active
May 18, 2021 08:11
-
-
Save sunriseweb/7588169 to your computer and use it in GitHub Desktop.
Get ACF (Advanced Custom Field) field groups with their fields for a given custom post type. i.e. where the ACF location rule is "post_type == cpt".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns an array of CPT's where an field group ACF location rule of "post_type == CPT" exists. | |
* - each CPT points at an array of its field group IDs, in turn pointed at an array of that field group's: | |
* - meta: field group meta [ id, title, menu_order ] | |
* - fields: array of info for each field [ key, label, name, type, order_no, instructions, required, id, class, conditional_logic[array()], etc. ] | |
* | |
* @since 1.0.0 | |
*/ | |
public function get_acf_field_groups_by_cpt() { | |
// need to create cache or transient for this data? | |
$result = array(); | |
$acf_field_group_manager = new acf_field_group(); | |
$all_field_groups = $acf_field_group_manager->get_field_groups(array()); | |
foreach($all_field_groups as $field_group) { | |
$group_locations = $acf_field_group_manager->get_location( '', $field_group['id'] ); | |
foreach($group_locations as $group_location_rules) { | |
foreach($group_location_rules as $rule) { | |
if($rule['param'] == 'post_type' && $rule['operator'] == '==') { | |
$result[$rule['value']][$field_group['id']]['meta'] = $field_group; | |
$result[$rule['value']][$field_group['id']]['fields'] = $acf_field_group_manager->get_fields( '', $field_group['id']); | |
} | |
} | |
} | |
} | |
return $result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Accepts a CPT strings as input. | |
* Returns an array of field group IDs point at an array of their: | |
* - meta: field group meta [ id, title, menu_order ] | |
* - fields: array of info for each field [ key, label, name, type, order_no, instructions, required, id, class, conditional_logic[array()], etc. ] | |
*/ | |
function get_cpt_field_groups($cpt) { | |
$result = array(); | |
$acf_field_group_manager = new acf_field_group(); | |
$all_field_groups = $acf_field_group_manager->get_field_groups(array()); | |
foreach($all_field_groups as $field_group) { | |
$group_locations = $acf_field_group_manager->get_location( '', $field_group['id'] ); | |
foreach($group_locations as $group_location_rules) { | |
foreach($group_location_rules as $rule) { | |
if($rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == $cpt) { | |
$result[$field_group['id']]['meta'] = $field_group; | |
$result[$field_group['id']]['fields'] = $acf_field_group_manager->get_fields( '', $field_group['id']); | |
} | |
} | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment