Support

Account

Home Forums General Issues How to get the Field Group ID from a page?

Solved

How to get the Field Group ID from a page?

  • I created many field groups and used ‘Show this field group if.. Page is equal to.. X‘.

    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.

  • I’m looking for this exact same thing. Currently I’m doing something like this, `<?php
    //or insert the ID of your fields Group.
    $groupID=’2480′;
    $custom_field_keys = get_post_custom_keys($groupID);
    $items = count($custom_field_keys);
    foreach ( $custom_field_keys as $key => $fieldkey )
    {
    if (stristr($fieldkey,’field_’))
    {
    //echo $key . ” => ” . $fieldkey . “<br />”;
    //echo $field[‘label’] . ‘: ‘ . $field[‘value’];
    //echo $fieldkey . “<br />”;
    $field = get_field_object($fieldkey, $groupID);

    echo ‘<span class=”h4″>’ . $field[‘label’] . ‘</span><br /><span class=”sans”>’ . $field[‘value’]. ‘</span></p>’;

    }
    }
    ?>`

    But would love to know how to get it from the post/page id.

  • Actually I wonder if this is how we get the fields lables/values with post id, http://www.advancedcustomfields.com/resources/functions/get_field_objects/ ??? I use that but got bool(false)? Whats up?

  • Okay so thats how you do it. http://www.advancedcustomfields.com/resources/functions/get_field_objects/
    This worked for me to get fields’ labels and values based on the postid, but

    Still not sure how to get the groupid from the post/page id.

    Good Luck!

  • I had to do it manually.

    I query the database and look for a rules for a specific post. If I found one, I grad the ID.

    It works, but it’s not pretty:

    //We look into the rules to see if any are applied
    $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; }
    }

    //Here, we read the fields since $numberofacffield is the ID we are looking for.
    $custom_field_keys = get_post_custom_keys($numberofacffield);

  • Hi Etienne,

    I have the same requirement as you…kind of. And I am stumped. I was going though the code that worked for you, and I have a question. At the cost of appearing a dud…here goes the question 🙂
    In the query here, is “rule” the name of your field group?

    Many Thanks,
    Sarah

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.