0

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

11
  • 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? Commented Apr 4, 2023 at 7:20
  • the $termID output is correct. It returns term_id as int. What makes me confuse is when i'm using wp_delete_term on category or tags is worked Commented Apr 4, 2023 at 7:41
  • I tried wp_delete_term(50, 'program'), . where 50 is the term_id for specific term in my custom taxonomy. it also fails Commented Apr 4, 2023 at 7:45
  • "It always return false" - how did you confirm it returns false? What code you use to check for that value? (Presumably, it's the callback hooked on qm/debug) And just to be sure, are you completely certain that delete_program_term_automatically is being executed? Are you also certain that program is the correct taxonomy? Have you tried with other registered custom taxonomies - did it work?
    – Sally CJ
    Commented Apr 4, 2023 at 8:25
  • 1
    The log method you made is big help to me. Finally, I know the problem was slug of the the post that become [slug]_trashed. So i just need to remove _trashed to match the category slug. Thank you @SallyCJ Commented Apr 8, 2023 at 2:45

1 Answer 1

0

Finally I get the solution for the code. Thank you my friend @SallyCJ for helping me out.

Here is the code:

add_action( 'publish_program-peduli', 'add_program_term' );
add_action( 'deleted_post', 'delete_program_term');

function add_program_term( $post_ID ) {
    $post = get_post( $post_ID ); // get post object
    wp_insert_term( $post->post_title, 'program' );
}

function delete_program_term($post_ID) {
    $cat = get_post($post_ID);
    if ( ! $cat || 'program-peduli' !== $cat->post_type ) { return; }
    $termslug = $cat->post_name;
    $queryTerm = get_term_by( 'slug', substr($termslug, 0, -9) , 'program');
    $termID = $queryTerm->term_id;
    wp_delete_term($termID, 'program');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.