1

I'm running a function that hooks into wp_trash_post and that should delete a custom taxonomy term that is the match of a custom post.

The term description equals the ID of the custom post.

  
function trash_custom_terms($post_id) {
  if ( 'custom_post' != get_post_type($post_id)) {
    return;
  }

$this_post_ID = get_post( $post_id )->ID;

  $existing_terms = get_terms('', array(
    'hide_empty' => false
    )
  );

  foreach($existing_terms as $term) {
    if ($term->description == $this_post_ID) {
      if (wp_delete_term($term->term_id, 'custom_taxonomy')){
            write_log('should be deleted');
      }else{
            write_log('isnt deleted');
      }
    }
  }
}
add_action('wp_trash_post', 'trash_custom_terms');

As can be seen in my code I am using a function to write to the WP debug log.

When the code runs and the matching custom post is deleted, the ID taxonomy term increments by 1! It is as if it is deleted then replaced by an identical term.

I cant figure out what is going on and have checked and double checked that the vars are correct, and they are.

Could anybody please shed some light as to why this may be?

Cheers

Noel

2 Answers 2

1

I'm not sure why you're seeing the ID increment, however one problem is that your function call to get_terms() needs to include an array of taxonomy names as the first parameter.

In your case, you just need to include an array with a single taxonomy name. For example:

$existing_terms = get_terms(array('custom_taxonomy'), array(
        'hide_empty' => false
    )
);

When I ran your sample (without this array) I was getting an error response from the get_terms() function.

Adding this array seemed to work as you described. More information on how to use get_terms().

1
  • cheers for the heads up, have altered my code as per your advice. On inspection of my code, and the fact that you said it works, I could see that the problem was the function I used for creating the terms using the save_post hook.
    – noelmcg
    Commented May 10, 2014 at 15:40
0

After inspected my code and the logs I could see that the problem was with the function I used to create a new taxonomy term when a new custom post was added.

The code would only check for get_post_status = 'auto-draft' and would still run and recreate the term when get_post_status = 'trash'


function update_custom_terms($post_id) {
  //checked if correct post
  //only checked if it wasn't an auto draft
  if (get_post_status($post_id) == 'auto-draft') {
    return;
  }
  //carried out rest of function ...
}
add_action('save_post', 'update_custom_terms');

Schoolboy error. Didn't realise save_post would run when the post was sent to the trash.

Updated code with the following:


  if ((get_post_status($post_id) == 'auto-draft') || (get_post_status($post_id) == 'trash') ) {
    return;
  }

Lesson learnt!

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.