I am trying to use transition_post_status with custom post statuses to update user meta each time a post status is updated. The following works when editing the post status in Edit Post Screen, but does not work when changing post status in Quick Edit in the admin view, which makes it difficult for the admin to change post statuses so that the change reflects in the user meta.
Is there something I might have missed?
function update_hit_rate( $new_status, $old_status, $post ) {
if ( $new_status != $old_status ) {
global $post;
$user_id = $post->post_author;
$args = array(
'author' => $user_id,
'posts_per_page' => -1,
'post_type' => 'betting_tip',
'post_status' => array('hit','miss')
);
$posts = get_posts( $args );
$hits = $misses = array();
if ( ! empty( $posts ) ) :;
foreach ( $posts as $post ) {
switch ( $post->post_status ) {
case 'hit':
$hits[] = $post;
break;
case 'miss':
$misses[] = $post;
break;
default:
break;
}
}
endif;
$hits = count($hits);
$misses = count($misses);
$hit_rate = round($hits / ($hits + $misses) * 100);
update_user_meta( $user_id, 'hit_rate', $hit_rate);
}
}
add_action( 'transition_post_status', 'update_hit_rate', 10, 3 );
EDIT: I am using the plugin Extended Post Status to add custom post statuses and status change options for the admin view. I also tried it with fully custom code, but the result is the same. User meta is updated from Edit Post screen, but from the Quick Edit menu it does nothing, even if the post status is changed.