Trong đoạn code sau đây, thay post type và category tương ứng
add_action('init', 'init_make_taxonomy_from_posts');
function init_make_taxonomy_from_posts(){
make_taxonomy_from_posts('post_type', 'category'); //Thay post type và category tương ứng
}
/**
* Create slugs/terms in jjm_author_tax taxonomy for all jjm_authors posts
*/
function make_taxonomy_from_posts($post_type, $taxonomy){
/** Grab the relevant posts */
$args = array(
'post_status' => 'publish',
'post_type' => $post_type,
'posts_per_page' => -1
);
$my_query = new WP_Query($args);
/** Check that some posts were found and loop through them */
if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();
/** Check to see if a term with the same title as the post currently exists in $taxonomy */
if(!term_exists(get_the_title(), $taxonomy)) : // It doesn't
/** Add a new term to $taxonomy */
$args = array(
'description' => get_the_content()
);
wp_insert_term(get_the_title(), $taxonomy);
endif;
endwhile;
/** Reset the '$post' global */
wp_reset_postdata();
endif;
}