Support

Account

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

  • 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;
    }