Question

I created many field groups and used 'Show this field group if.. Page is equal to.. X' with the famous Advanced Custom Fields (ACF) plugin.

I'm using get_post_custom_keys() to show all the custom fields from a page:

$custom_field_keys = get_post_custom_keys(45);

I hardcoded the '45' which is not the page ID, but the Field Group ID. I'm struggling to get the ID of the field group associated with the page.

get_post_custom_keys($post_id); will show the custom fields for the page and not the field group.

I understand there might be multiple field groups associated with one page.

Was it helpful?

Solution

Here is my solution. I look into the database directly and look for any ACF rules for the current page, and grab that ID.

//Look for ACF rules for the current post
$rows = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE meta_key = 'rule'");
  foreach ($rows as $row) {
    $values = unserialize($row->meta_value);                            
    if ($postid == $values["value"]) { $numberofacffield = $row->post_id; }
}

//Then read fields for the acf group id : $numberofacffield 
$custom_field_keys = get_post_custom_keys($numberofacffield);

OTHER TIPS

Since ACF 5.0 you can use this function.

$post_id = get_the_ID();
$groups = acf_get_field_groups([
  'post_id' => $post_id,
]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top