Support

Account

Home Forums ACF PRO Get all field groups related to post

Solved

Get all field groups related to post

  • Is there a ACF function to retrieve field groups (not fields) by post id? I thought I’ve seen something similar before, but can’t find it. get_field_objects() retrieves all fields, but doesn’t separate them by field group. Any ideas?

  • $groups = acf_get_field_groups($post_id'); will get the field groups, but does not include the fields. You then need to loop through the field groups and use $fields = acf_get_fields($group_key);

  • Thanks John.

    acf_get_field_groups is returning an empty array. If I remove the post_id then all field groups are returned.

    Is this documented somewhere? I also saw that some filters changed to functions on v5, but I haven’t found any new documentation.

  • These functions are not documented. They are basically internal ACF functions that I know about from digging around in the code. This is how ACF gets the field groups and fields. Using them at the wrong time can also cause issues if you’re not careful. In particular, if you want to change the settings of a field group or field after you’ve called these functions you’ll need to manually clear the cached versions. These functions store the results in a cache so that the fields do not need to be retrieved from the database again. But since the values are cached any changes to them are not updated.

  • So.. I removed the $post_id and all field groups are returned. With the post_id no field groups are returned. I hardcoded the ID so that I was sure the correct value was being passed, but I still get an empty array. I am not trying to modify the values. I just want to loop through each field group, and print a different layout for each one.

  • I am looking through ACF’s code. The function that supposed to return the field groups for an ID is:

    /*
    * acf_get_field_group
    *
    * This function will take either a post object, post ID or even null (for global $post), and
    * will then return a valid field group array

    But that’s not returning anything. Not even an empty array.

  • This is pretty confusing… but it looks like “post object”, “post id” or “global $post” in the function’s description are expected to be an ACF Group Field ID, object, global… and NOT the post ID where the group is being used. So for example, not the home page ID.

    “This function will take either a post object, post ID or even null (for global $post)”

    And acf_get_field_groups() accepts an array of arguments, so passing $post_id/$post->ID won’t return anything. The default arguments indicate that’s also looking for all field_groups that have been published.

    I can’t seem to find a function, that given a $post->ID will return all related field groups that are being used in that post.

    My ultimate goal is to:

    -> on page load, get’s id ($post->ID)
    -> retrieve all field groups that have been used in this page
    -> and then loop through each group, printing a different layout for each group.

    I can do this easily right now using the “Flexible Content” field, because when I call get_fields, each layout has specific name, that I can check against.

  • I had to do some digging, I know I’ve done this before.

    You need to pass the post ID as an argument like this
    $groups = acf_get_field_groups(array('post_id' => $post_id);

  • John, thank you very much. That worked. I had already moved on and tried other approaches, like getting all field groups and then filtering them out by location rule. But this is a lot easier.

  • Actually, what you were going to try to do is what calling the function with the post_id argument does. First it gets all the field groups and then it runs a filter on them then checks the location rules.

  • Yeah. I just didn’t realize it was already doing it for me.

  • It looks like you were trying to do something similar to what I’m trying to do, could you take a look at my post and give me a hand?

    https://support.advancedcustomfields.com/forums/topic/simple-fields-to-acf/

  • Here, I wrote a function that side-steps the ACF library and just grabs them from the database, just use it using post_type

    You can get post_type from ID with get_post_type( $ID )

    function get_acf_fields_post_type( $post_type )
      {
          global $wpdb;
    
          $sql = "SELECT p.ID, p.post_title, p.post_name, pm.meta_value as rule
                  FROM $wpdb->posts p 
                  LEFT JOIN $wpdb->postmeta pm 
                  ON ( p.ID = pm.post_id AND pm.meta_key = 'rule' ) 
                  WHERE p.post_type = 'acf'";
    
          $result = $wpdb->get_results($sql);
    
          $groups = array();
    
          foreach($result as $row){
    
            $rule = unserialize($row->rule);
    
            if( $rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == $post_type )
            { 
              $groups[$row->ID] = array('title'=>$row->post_title,'name'=>$row->post_name);
            }
    
          }
    
          foreach($groups as $post_id => $data)
          {   
              $fsql = "SELECT * FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key LIKE 'field_%';";
    
              $fields = $wpdb->get_results($fsql);
    
              $field_array = array();
              foreach($fields as $field)
              {
                $f = unserialize($field->meta_value);
                $field_array[$field->meta_key] = $f; 
              }
              $groups[$post_id]['fields'] = $field_array;
          }
    
          return $groups;
    
      }
  • Grabbing the field groups from the database will not work if they do not exist in the database. This can be done by using local JSON files for groups. https://www.advancedcustomfields.com/resources/local-json/

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

You must be logged in to reply to this topic.

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.