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'];
?>
$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();
?>