I have created custom taxonomy called program using Custom Post Type Plugin. I want to create functions using add_action that can create and delete term which has the same name as the post automatically whenever the post is published or trashed.
Here are the code:
add_action( 'publish_program-peduli', 'add_program_term' );
add_action('deleted_program-peduli', 'delete_program_term_automatically');
function delete_program_term_automatically($post_ID) {
$cat = get_post($post_ID);
$termslug = $cat->post_name;
$queryTerm = new WP_Term_Query( array('taxonomy' => 'program', 'slug' => $termslug, 'hide_empty' => false ));
$termID = $queryTerm->get_terms()[0]->term_id;
do_action( 'qm/debug', wp_delete_term($termID, 'program'));
}
function add_program_term( $post_ID ) {
$post = get_post( $post_ID ); // get post object
wp_insert_term( $post->post_title, 'program' );
do_action( 'qm/debug', $post->post_title);
}
the create term function is working, but not for the remove term. It always return false. Please help
false
means that the term doesn't exist. Have you verified that$termID
is what you expect it to be? What about the other variables?wp_delete_term(50, 'program'),
. where 50 is the term_id for specific term in my custom taxonomy. it also failsfalse
? What code you use to check for that value? (Presumably, it's the callback hooked onqm/debug
) And just to be sure, are you completely certain thatdelete_program_term_automatically
is being executed? Are you also certain thatprogram
is the correct taxonomy? Have you tried with other registered custom taxonomies - did it work?[slug]_trashed
. So i just need to remove_trashed
to match the category slug. Thank you @SallyCJ