Monday, December 19, 2022
HomeWordPress Developmenthooks - wp_trash_post not firing as anticipated (additionally applies to trashed_post)

hooks – wp_trash_post not firing as anticipated (additionally applies to trashed_post)


That is much like wp_trash_post not firing however follows on from there.

I’ve a bit of code to run on this hook;

operate to_fire_on_hook($post_id) {

 ... do stuff

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

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

    // does not work until I put 
    die; //right here
   
  }
}

add_action('wp_trashed_post', 'to_fire_on_hook');

if I put die; within the operate completes OK, if I take away it the operate does not full.
I assumed it is perhaps to do with the truth that placing the put up within the trash appends _trash to its title however appending that to $slug does not make any distinction.

Any gentle gratefully acquired.

Because the title, I’ve additionally used the trashed_post hook.

Here is the entire of my code, in case it is one thing in there.

// Delete phrases on trash put up for date posts

operate delete_terms_from_product($post_id) {

  if ('date' !== get_post_type() ) {
    return;
  } else {
    // hook firing?
    //echo "Hooked";die;
    $put up = get_post($post_id);
    $slug = $post->post_name;
    //echo $slug; // Test
    $product_id = get_field('associated_product' , $post_id);
    //echo $product_id; // Test
    //print_r(get_the_terms($product_id,'pa_dates')); // simply to ensure we're wanting on the proper issues, test
    wp_remove_object_terms($product_id, $slug, 'pa_dates');
   // works completely if the subsequent line is uncommented. Doesn't work whether it is commented 
   //wp_die();
  }
}

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments