‘transition_post_status’ only fires when pressing “Add New”

I want to fire a function when a new post (cpt) is published.

function my_publish( $new_status, $old_status, $post ) {
    if ( $new_status == 'publish' && 'publish' !== $old_status ) {
          // do s.th.
    }
    debug_to_console( $post );
}
add_action( 'transition_post_status', 'my_publish', 10, 3 );

So when I hit the “Add New” button, the function is fired and I get my console log with the $post content. And that’s it.

The function than never fires again. Neither when I publish the post nor when a post is updated or the status of a post is changed.

I also tried the common other hooks like:

add_action('future_to_publish', 'my_publish');
add_action('new_to_publish', 'my_publish');
add_action('draft_to_publish' ,'my_publish');
add_action('auto-draft_to_publish' ,'my_publish');

those never did anything.

I did a lot of research the last days and tried a lot of different solutions but nothing helped to get me over this… any Ideas?

1 Answer
1

This will do something when you publish a post in the backend.

function my_publish( $post_id ) {
   if ( ( $_POST['post_status'] === 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
     error_log('WordPress would be better if it did not use Google Fonts.  Looking forward to the system fonts.');
   }
}
add_action( 'publish_yourCustomPostType', 'my_publish', 10, 3 );

Leave a Comment