Support

Account

Home Forums General Issues get list of field groups for location rule

Solved

get list of field groups for location rule

  • Hi,
    I’m using ACF to record information against posts for the cpt “businesses”. I’ve split these into various field groups like “Business Info”, “Offers”, “Rewards”, “CRM”, etc.

    When the Business information changes my plan is to create a post in the cpt “activitylogs” with information specific to the field group where the activity occurred.

    So, when a post is saved (including create) I want to hook in, figure out what fields have changed, and then create Activity Log posts as required.

    To do this I was going to use: add_action('acf/save_post', 'add_activitylogs', 1); (before changes saved) to loop through the new $_POST values and compare them to the existing post values (e.g. using get_field('field_name', $post_id); ).

    The problem is I’m not sure how to get the field-to-field-group relationship without having to hard-code it?

    The following will allow me to get specify the field_group_id to get the fields which belong – so at least then I would only need to hard-code the field groups.

    $fields = apply_filters('acf/field_group/get_fields', array(), $field_group_id);

    How can I get the ACF field groups with an ACF “Location” where Post Type is equal to my CPT (“businesses” in this case)?

    Thanks for a great plugin, and the help!
    Brad Trivers

  • Hi @sunriseweb

    This is getting pretty custom – nice.

    I would look through the core code in acf.php and core/controllers/input.php to find references to ACF matching location rules and finding field groups for a given post.

    Good luck!

    Thanks
    E

  • Ok – I think I’m on the right track:

    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;
    }
  • Thanks for the code Sunrise. I just ran into the same problem.

  • Just updated to do something similar for ACF5. Much easier thanks to the API improvements. Thanks!

    /**      
     * 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;
    }
  • More easy & efficient way,
    //Fields groups assigned to post type “Foo”
    $groups = acf_get_field_groups( array( 'post_type' => 'Foo' ) );
    //Fields groups assigned to user role “Test”
    $groups = acf_get_field_groups( array('user_role' => "Test") );

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

The topic ‘get list of field groups for location rule’ is closed to new replies.