hooks – wp_trash_post not firing as expected (also applies to trashed_post)

Question

This is similar to wp_trash_post not firing but follows on from there.

I have a piece of code to run on this hook;

function to_fire_on_hook($post_id) {

 ... do stuff

// Get the slug of the $post_id above - the post being trashed
    $slug = $post->post_name;

... more stuff that then
    do_something(..., $slug, ...);

    // doesn't work unless I put 
    die; //here
   
  }
}

add_action('wp_trashed_post', 'to_fire_on_hook');

if I put die; in the function completes OK, if I remove it the function doesn’t complete.
I thought it might be to do with the fact that putting the post in the trash appends _trash to its name but appending that to $slug doesn’t make any difference.

Any light gratefully received.

As the title, I’ve also used the trashed_post hook.

Here’s the whole of my code, in case it’s something in there.

// Delete terms on trash post for date posts

function delete_terms_from_product($post_id) {

  if ('date' !== get_post_type() ) {
    return;
  } else {
    // hook firing?
    //echo "Hooked";die;
    $post = get_post($post_id);
    $slug = $post->post_name;
    //echo $slug; // Check
    $product_id = get_field('associated_product' , $post_id);
    //echo $product_id; // Check
    //print_r(get_the_terms($product_id,'pa_dates')); // just to make sure we're looking at the right things, check
    wp_remove_object_terms($product_id, $slug, 'pa_dates');
   // works perfectly if the next line is uncommented. Does not work if it is commented 
   //wp_die();
  }
}

add_action('pre_trash_post', 'delete_terms_from_product', 999, 2);

0
Chris Pink 1 month 2022-12-18T11:30:19-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse