Skip to content

Instantly share code, notes, and snippets.

@herewithme
Created December 3, 2013 16:20
Show Gist options
  • Save herewithme/7772173 to your computer and use it in GitHub Desktop.
Save herewithme/7772173 to your computer and use it in GitHub Desktop.
Small class helper for get ACF fields for a specific post type
<?php
class BEA_ACF_Helpers {
// Fields to not add to the available fields of acf
private static $unauth_fields = array(
'repeater',
'relationship',
'flexible_content'
);
/**
* Get the ACF fields for current post type
*/
public static function get_fields( $post_type = 'post', $field_name = null ) {
global $acf_register_field_group;
$fields = array();
// Parse the acf fields
foreach( $acf_register_field_group as $field_group ) {
if( empty( $field_group ) || ( !empty( $post_type ) && !self::is_post_type_in_group( $field_group['location'], $post_type ) ) ) {
continue;
}
foreach ( $field_group['fields'] as $field ) {
// Skip the not really usable fields
if( in_array( $field['type'], self::$unauth_fields ) ) {
continue;
}
if ( $field_name == null ) {
$fields[$field['key']] = $field;
} else {
$fields[$field['key']] = $field[$field_name];
}
}
}
return $fields;
}
public static function is_post_type_in_group( $location_array, $post_type ) {
foreach( $location_array[0] as $location ) {
// Skip the not post_type array
if( $location['param'] != 'post_type' || $location['operator'] !== '==' ) {
continue;
}
// If we have the post_type, then set it as true
if( $location['value'] == $post_type ) {
return true;
}
}
return false;
}
}
@Rahe
Copy link

Rahe commented Dec 12, 2013

Voilà la class qui fonctionne correctement et avec les champs enregistrés en base :

<?php
class BEA_ACF_Helpers {
    // Fields to not add to the available fields of acf
    private static $unauth_fields = array(
        'repeater',
        'relationship',
        'flexible_content'
    );

    /**
     * Get the ACF fields for current post type
     */
    public static function get_fields( $filters = array( 'post_type' => 'post' ), $field_name = null ) {
        global $acf_register_field_group;

        // Get tehe fields matching the post_type
        $matching_group_fields = apply_filters( 'acf/location/match_field_groups', false, $filters );

        // Return empty data if nothing found for this location
        if( empty( $matching_group_fields ) ) {
            return array();
        }

        // Make array basesc fields
        $final_fields = array();

        // Get the ACF fields
        $acfs = apply_filters( 'acf/get_field_groups', array() );

        // Get all the group and fields data
        foreach( $acfs as $index => $acf ) {

            if( !in_array( $acf['id'], $matching_group_fields ) ) {
                continue;
            }

            $acf['fields'] = apply_filters( 'acf/field_group/get_fields', array(), $acf['id'] );
            $acf['options'] = apply_filters( 'acf/field_group/get_options', array(), $acf['id'] );
            $acf['location'] = apply_filters( 'acf/field_group/get_location', array(), $acf['id'] );
            $final_fields[] = $acf;
        }


        // Merge the two arrays
        $final_fields = $acf_register_field_group+$final_fields;

        // Init returned array
        $fields = array();

        // Parse the acf fields
        foreach( $final_fields as $field_group ) {

            if( empty( $field_group ) ) {
                continue;
            }

            foreach ( $field_group['fields'] as $field ) {
                // Skip the not really usable fields
                if( in_array( $field['type'], self::$unauth_fields ) ) {
                    continue;
                }

                if ( $field_name == null ) {
                    $fields[$field['key']] = $field;
                } else {
                    $fields[$field['key']] = $field[$field_name];
                }
            }
        }

        return $fields;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment