Skip to content

Instantly share code, notes, and snippets.

@stefanledin
Last active October 2, 2019 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanledin/44b20ddde3f8df8583c989412d184c49 to your computer and use it in GitHub Desktop.
Save stefanledin/44b20ddde3f8df8583c989412d184c49 to your computer and use it in GitHub Desktop.
<?php
/**
* Register the custom taxonomy.
*/
add_action( 'init', 'register_taxonomy_article_type' );
function register_taxonomy_article_type() {
register_taxonomy( 'article_type', 'post', array(
'label' => 'Article type',
'hierarchical' => true,
// This taxonomy should only be visible in the admin menu.
'public' => false,
'show_ui' => true,
'meta_box_cb' => false,
) );
}
/**
* Populate the ACF field with terms from the custom taxonomy Article type.
*/
add_filter( 'acf/load_field/name=article_type', function( $field ) {
// Get all taxonomy terms
$article_types = get_terms( array(
'taxonomy' => 'article_type',
'hide_empty' => false
) );
// Add each term to the choices array.
// Example: $field['choices']['review'] = Review
foreach ( $article_types as $type ) {
$field['choices'][$type->slug] = $type->name;
}
return $field;
} );
@shisan79
Copy link

shisan79 commented Oct 2, 2019

nice. how to sync it with taxonomy to update it based on acf select field?

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