How can I hook into existing WordPress Bulk actions?

As you describe, you want to sync data when a post is trashed, edited or untrashed. You are trying with save_post action, but it fires only on post edit screens (according with codex, this action fires on import, post/page edit form, xmlrpc, or post by email), so you think you need to hook also on bulk actions and quick edit but you are wrong. There are not specific saving actions that are triggered when a post is edited through bulk actions or quick edit (the link you posted about adding custom bulck actions is for that, to add custom bulk actions, not for perform tasks on predefined bulk actions).

From my point of view, there is nothing more appropiate for post status transitions than syncing data.

You have tried post status transitions but with wrong logic. For example, you run de delete sync function when a post transits from be published to not published, but this situation doesn’t mean a post is deleted: a not published post can be trashed, a draft, a future post, etc.

Let’s see some examples:

add_action( 'transition_post_status', 'cp_sync', 10, 3 );
function cp_sync( $new_status, $old_status, $post ) {

    // Check $old_status also if you need specific actions
    // when the post transits from a specific status

    if ( $new_status == 'draft' ) {
        // The post is now draft, no matter what status it had previously
        // sync draft posts here
    }

    if ( $new_status == 'publish' ) {
        // The post is now published, no matter what status it had previously
       // sync published posts here
    }

    if ( $new_status == 'trashed' ) {
        // The post is now in the trash, no matter what status it had previously
        // sync trashed posts here
    }

    // Cotinue checking more post statues if you need (future, private, etc)
    // For a complete list, see https://codex.wordpress.org/Post_Status_Transitions 

}

There is not status transition for deleted post, you must use any of the actions described in the docs for delete_post action; I would use before_delete_post if you need to catch all the post data:

add_action( 'before_delete_post', 'my_func' );
function my_func( $postid ){
    // Post has been deleted
    pluginname_sync::pluginname_delete( $postid );
}

EDIT

Exmpale: Sync only published posts, delete the rest from external database:

add_action( 'transition_post_status', 'cp_sync', 10, 3 );
function cp_sync( $new_status, $old_status, $post ) {

    // This will cover the transition from any status to published,
    // including published to published.
    if ( $new_status == 'publish' ) {
        pluginname_sync::pluginname_syncpost($post->ID);
    } else {
        pluginname_sync::pluginname_delete($post->ID);
    }

}
add_action( 'before_delete_post', 'my_func' );
function my_func( $postid ){
    pluginname_sync::pluginname_delete( $postid );
}

Leave a Comment