Created
December 3, 2013 16:20
-
-
Save herewithme/7772173 to your computer and use it in GitHub Desktop.
Small class helper for get ACF fields for a specific post type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Voilà la class qui fonctionne correctement et avec les champs enregistrés en base :