CPT’s Taxonomy Metabox Getting Repeated on Edit/Add Admin

I have created a CPT, with 2 Taxonomies. Tax 1 contains 4 set default terms, Tax 2 contains 3. When I add the metabox for them to the add/edit pages, both taxonomy’s terms are added to each

How do I get the proper terms into the proper metabox for the proper taxonomy?

Here is the code running it:

// Categories
$this -> create_the_term( ‘alert_type’, ‘Category’ );

// Alert Activity
$this -> create_the_term( ‘alert_activity’, ‘Activity’ );

// Create our category terms
$this -> create_category_terms( );

// Create the terms
private function create_the_term( $term_name, $term_label ) {
register_taxonomy(
$term_name, array( ‘gyo_alerts’, ),
array(
‘hierarchical’=> true,
‘label’ => $term_label,
‘singular_label’ => $term_label,
‘rewrite’ => false,
‘publicly_queryable’ => false,
‘public’ => false,
‘show_ui’ => true,
‘show_in_menu’ => false,
‘show_in_nav_menus’ => false,
‘show_admin_column’ => true,
‘capabilities’ => array( // we need most of the display options, but do not want these to be edittable
‘assign_terms’ => ‘manage_options’,
‘edit_terms’ => ‘god’,
‘manage_terms’ => ‘god’,
),
‘show_in_quick_edit’ => false,
‘meta_box_cb’ => function( $post ) { // change these to single-select
$terms = get_terms( $term_name, array( ‘hide_empty’ => false ) );
$post = get_post();
$_terms = wp_get_object_terms( $post->ID, $term_name, array( ‘orderby’ => ‘term_id’, ‘order’ => ‘ASC’ ) );
$name = ”;
if ( ! is_wp_error( $_terms ) ) {
if ( isset( $_terms[0] ) && isset( $_terms[0]->name ) ) {
$name = $_terms[0]->name;
}
}
foreach ( $terms as $term ) {
?>
<label title='<?php esc_attr_e( $term->name ); ?>’>
<input type=”radio” name=”<?php esc_attr_e( $term_name ); ?>” value=”<?php esc_attr_e( $term->name ); ?>” <?php checked( $term->name, $name ); ?> />
<span><?php esc_html_e( $term->name ); ?></span>
</label><br />
<?php
}
unset( $terms, $_terms );
},
)
);
}

// Create our taxonomy’s terms
private function create_category_terms( ) {
// define our category terms
$term = term_exists( ‘all-clear’, ‘alert_type’ );
if ( $term === 0 || $term === null ) {
wp_insert_term( ‘All Clear’, ‘alert_type’, array(
‘slug’ => ‘all-clear’,
) );
}
unset( $term );
$term = term_exists( ‘info’, ‘alert_type’ );
if ( $term === 0 || $term === null ) {
wp_insert_term( ‘Info’, ‘alert_type’, array(
‘slug’ => ‘info’,
) );
}
unset( $term );
$term = term_exists( ‘warning’, ‘alert_type’ );
if ( $term === 0 || $term === null ) {
wp_insert_term( ‘Warning’, ‘alert_type’, array(
‘slug’ => ‘warning’,
) );
}
unset( $term );
$term = term_exists( ’emergency’, ‘alert_type’ );
if ( $term === 0 || $term === null ) {
wp_insert_term( ‘Emergency’, ‘alert_type’, array(
‘slug’ => ’emergency’,
) );
}
unset( $term );
// define our category activity
$term = term_exists( ‘active’, ‘alert_activity’ );
if ( $term === 0 || $term === null ) {
wp_insert_term( ‘Active’, ‘alert_activity’, array(
‘slug’ => ‘active’,
) );
}
unset( $term );
$term = term_exists( ‘temporary’, ‘alert_activity’ );
if ( $term === 0 || $term === null ) {
wp_insert_term( ‘Temporary’, ‘alert_activity’, array(
‘slug’ => ‘temporary’,
) );
}
unset( $term );
$term = term_exists( ‘non-active’, ‘alert_activity’ );
if ( $term === 0 || $term === null ) {
wp_insert_term( ‘Not Active’, ‘alert_activity’, array(
‘slug’ => ‘non-active’,
) );
}
unset( $term );
}

Read more here:: CPT’s Taxonomy Metabox Getting Repeated on Edit/Add Admin

Leave a Reply

Your email address will not be published. Required fields are marked *