I am trying to figure out to fire a hook for a custom post type called "bananas" when a new bananas post is published.
Requirements:
- Can't use $_POST
- Need to be able to get post statuses so I can prevent code from running again later and check if its already published
- Need to be able to get_post_meta
The action hook is working perfectly. The only issue is that NEW post can't seem to get_post_meta. If you go from pending to publish or vice versa getting the meta works. But getting the meta on new post does not work and returns an empty result.
Heres an example of what I am trying to do.
class bananas {
public function __construct() {
add_action( 'transition_post_status', array( $this, 'email_bananas_published' ), 10, 3 );
}
public function email_bananas_published( $new_status, $old_status, $post ) {
if ( $post->post_type === 'bananas' && $new_status !== $old_status ) {
$email = get_post_meta( $post->ID, '_bananas_email', true );
error_log( $email );
}
}
}
I have been stuck on this for a while and any help is much appreciated.
$_POST
as suggested in the answer by Palmer Del Campo below.