I have a custom post type that always do math operation to its post_meta and other custom post type post_meta.

For example:

  • post-type 1 = cpt_product_order
  • post-type 1 post_meta = cpt_pm_product_order
  • post-type 2 = cpt_product
  • post-type 2 post_meta = cpt_pm_product_stock

There's a cpt_product with cpt_pm_product_stock = 100, frontend operation enable specific user made order by inserting new cpt_product with post_meta cpt_product_order of 10 will substract cpt_pm_product_stock to 90

100 cpt_pm_product_stock - 10 cpt_product_order = 90 cpt_pm_product_stock

cancelling order will delete/trash the cpt_product and cpt_product_order, and cpt_pm_product_stock should back to 100.

Which action hook should used for this operation? Is it trashed_post?

I didn't find any answer on codex, http://codex.wordpress.org/Plugin_API/Action_Reference/trashed_post is empty

Thanks in advance for an answer. ;)

  • Please use the code styling and links - we'll approve your edit in mins. Thanks.
    – kaiser
    Jul 3, 2012 at 16:09

2 Answers 2


wp_trash_post and trashed_post - from source:

function wp_trash_post($post_id = 0) {
    if ( !EMPTY_TRASH_DAYS )
        return wp_delete_post($post_id, true);

    if ( !$post = wp_get_single_post($post_id, ARRAY_A) )
        return $post;

    if ( $post['post_status'] == 'trash' )
        return false;

    do_action('wp_trash_post', $post_id);

    add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']);
    add_post_meta($post_id,'_wp_trash_meta_time', time());

    $post['post_status'] = 'trash';


    do_action('trashed_post', $post_id);

    return $post;
  • Is add_action to this wp_trash_post() hook, is it work when we use wp_delete_post() ? Jul 13, 2012 at 22:20
  • No, you'll also need to use before_delete_post (pre) or deleted_post (post) to listen out for deletions. Jul 13, 2012 at 23:03
  • wp_trash_post only seems to work on posts, not on pages or CPTs
    – urok93
    Sep 3, 2012 at 10:18

before_delete_post or wp_trash_post.

Both hooks are triggered before a post is deleted or trashed. You can use one of these hooks to perform the necessary operations when a post of cpt_product_order is deleted or trashed.

add_action('before_delete_post', 'restore_stock_on_order_cancellation');

function restore_stock_on_order_cancellation($post_id) {
    $post_type = get_post_type($post_id);

    // Check if the post being deleted is of the 'cpt_product_order' type
    if ($post_type === 'cpt_product_order') {
        // Get the order amount from post meta
        $order = get_post_meta($post_id, 'cpt_pm_product_order', true);

        // Get the product ID from post meta
        $product_id = get_post_meta($post_id, 'cpt_pm_product_id', true);

        // Increase the stock of the related product
        $current_stock = get_post_meta($product_id, 'cpt_pm_product_stock', true);
        $new_stock = $current_stock + $order;
        update_post_meta($product_id, 'cpt_pm_product_stock', $new_stock);

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.