0

When a 'contact' cpt post is trashed, I need a metakey from another cpt 'act' to be deleted, I have been searching all over which hook I should use but unfortunately the codex action reference page is not very helpful. Below is the code I have.

Ideally, I would also like an Admin Notice to be placed in the post listing screen where the user will be redirected to.

Question:

    1. is this the best hook to be using ?
    1. Why isn't it working as expected ?

Below the code I have thus far.

add_action( 'wp_trash_contact', 'contact_cpt_cleanup' );

function contact_cpt_cleanup( $post_id ){

global $post_type;   
    if ( $post_type != 'contact' ) return;

    // die('deleted contact');

    // check to see if the current contact has any acts attached
    $acts_associated = get_post_meta( $post_id, 'acts_associated', true );

    // if so, delete the association from the act to the contact
    if( $acts_associated ){
        foreach( $acts_associated as $act_id ) {
        delete_post_meta( (int) $act_id, 'contact_associated' );
        }
    }

}

Thanks.

EDITS:

Reading this post and this error report I now believe the correct syntax for this action hook is wp_{post_status}_{post_type} and not 'trash_post'. But it is still not working.

2

wp_trash_post is literal, not dynamic. It doesn't change and always ends in _post regardless of actual post type. See wp_trash_post():

do_action( 'wp_trash_post', $post_id );

You also should retrieve post and check its type from $post_id, do not use globals in that fashion because they might refer to entirely different post.

Your second theory refers to entirely different hook in wp_transition_post_status():

do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );

Note that it doesn't start with wp_, however it does contain specific post type in it dymaically, so it will be trash_contact.

  • looks like i had a few things mixed up, thanks for the info, will try your suggestions and update my post once its working. – Ronnieinspain Apr 20 '14 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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