Home Forums Backend Issues (wp-admin) How to get all ACF groups or field for specific post type


How to get all ACF groups or field for specific post type

  • Hi

    I’d like to know how do you get all the acf groups or acf fields for specific post type. I don’t mean for specific post, but post type.

    The same as done in a post edit screen


  • Try

    $groups = acf_get_field_groups(array('post_type' => 'your-post-type'));

    That will get the groups, I think. You then need to loop through the groups and use the group key to get the fields in each group using

    $fields = acf_get_fields($group_key);
  • Thanks
    Could be nice if you would provide a function that queries it all, no?

  • I didn’t build it and the functions are those used by ACF. I just know they exist from digging around in ACF and building plugins that use ACF.

  • This code is not working, I’ve tried it and it’s returning an empty array. I’ve trace the code and found out that the filter code always returns false when using outside of ACF.

    $groups = acf_get_field_groups(array('post_type' => 'your-post-type'));

    Filter code

    function acf_match_location_rule( $rule, $screen ) {
    	// vars
    	$result = false;
    	// filter
    	$result = apply_filters( "acf/location/rule_match/{$rule['param']}", $result, $rule, $screen );
    	$result = apply_filters( "acf/location/rule_match", $result, $rule, $screen );
    	// return
    	return $result;

    Hope someone can help further explore find a solution for this 🙂

  • posted this on another thread but might also be what you’re looking for

    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;
  • FYI, The original code (below) seems to be working for me now (ACF PRO v5.7.9)

    $groups = acf_get_field_groups(array('post_type' => 'your-post-type'));

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

The topic ‘How to get all ACF groups or field for specific post type’ is closed to new replies.