name = 'advanced_taxonomy_selector'; $this->label = __('Advanced Taxonomy Selector', 'acf-advanced-taxonomy-selector'); $this->category = __("Relational",'acf'); $this->defaults = array( 'taxonomies' => '', 'data_type' => 'terms', 'field_type' => 'multiselect', 'allow_null' => true, 'post_type' => false, 'return_value' => 'term_id' ); parent::__construct(); } /** * Field Options * * Creates the options for the field, they are shown when the user * creates a field in the back-end. Currently there are six fields. * * The Type sets the field to a term or a taxonomy selector * * The Taxonomies setting will put a restriction on the taxonomies shown * * The Restrict To Post Type setting allows you to restrict to taxonomies * defined for the selected post types * * The Field Type setting sets the type of field shown to the user * * By checking Allow Null you can make sure that users can select an empty * value * * The Return Value determines how the value is returned to be used on the * front end * * @param array $field The details of this field * @author Daniel Pataki * @since 3.0.0 * */ function create_options( $field ) { $key = $field['name']; ?> 'radio', 'name' => 'fields['.$key.'][data_type]', 'value' => $field['data_type'], 'choices' => array( 'terms' => __( 'Term Selector', 'acf-advanced-taxonomy-selector' ), 'taxonomy' => __( 'Taxonomy Selector', 'acf-advanced-taxonomy-selector' ), ) )); ?> 'select', 'name' => 'fields['.$key.'][taxonomies]', 'value' => $field['taxonomies'], 'multiple' => true, 'choices' => acfats_taxonomies_array() )); ?> 'select', 'name' => 'fields['.$key.'][post_type]', 'value' => $field['post_type'], 'choices' => acfatspost_types_array() )); ?> 'select', 'name' => 'fields['.$key.'][field_type]', 'value' => $field['field_type'], 'choices' => array( 'multiselect' => __( 'Multiselect', 'acf-advanced-taxonomy-selector' ), 'select' => __( 'Select', 'acf-advanced-taxonomy-selector' ) ) )); ?> 'radio', 'name' => 'fields['.$key.'][allow_null]', 'value' => $field['allow_null'], 'layout' => 'horizontal', 'choices' => array( 1 => __( 'Yes', 'acf-advanced-taxonomy-selector' ), 0 => __( 'No', 'acf-advanced-taxonomy-selector' ), ) )); ?> 'radio', 'name' => 'fields['.$key.'][return_value]', 'value' => $field['return_value'], 'choices' => array( 'term_id' => __( 'Term ID / Taxonomy Slug', 'acf-advanced-taxonomy-selector' ), 'object' => __( 'Term Object / Taxonomy Object', 'acf-advanced-taxonomy-selector' ), ) )); ?> $taxonomy ) { if( wp_count_terms( $slug ) == 0 ) { unset( $taxonomies[$slug] ); } } ?> '' ) ) { return ''; } return $value; } /** * Format Value * * This filter is appied to the $value after it is loaded from the * db and before it is passed to the create_field action * * @param mixed $value The value which was loaded from the database * @param int $post_id The $post_id from which the value was loaded * @param array $field The details of this field * @return mixed $value The modified value * @author Daniel Pataki * @since 3.0.0 * */ function format_value( $value, $post_id, $field ) { if( empty($value) ) { return $value; } if( $field['data_type'] == 'terms' ) { foreach( $value as $i => $val ) { $term = substr( $val, strrpos( $val, '_' ) + 1 ); if( $field['return_value'] == 'object' ) { $taxonomy = substr( $val, 0, strrpos( $val, '_' ) ); $term = get_term( $term, $taxonomy ); } $value[$i] = $term; } } elseif( $field['data_type'] == 'taxonomy' && $field['return_value'] == 'object' ) { foreach( $value as $i => $val ) { $value[$i] = get_taxonomy( $val ); } } return $value; } } // create field new acf_field_advanced_taxonomy_selector(); ?>