Am trying to send an email based on the contents of a post published but it should only be done if its the first time the post is published.
Therefore am using add_action( 'transition_post_status', [ $this, 'jackie_publish_post_hook'], 20, 3 );
the callback function jackie_publish_post_hook
has the following content.(The full content is quite big and some information is irrelevant to this question therefore i will only include the relative part)
public function jackie_publish_post_hook($new,$old,$post){
...
$accepted = array('pending','future','draft','auto-draft');
if(in_array($old, $accepted) && $new == 'publish'){
$attachment = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full', true);
print_r($attachment);
}
My problem occurs when I try to access the any post meta eg the featured image or any custom field as they have not been saved yet to database.
Some solutions have suggested using add_action( 'publish_post', ... );
and then accessing the values from the $_POST
variable but when I do this the $_POST
returns an empty array.
I have confirmed from the developers tools that the featured image id is being sent as a post parameter with the request but I cannot seem to access it from the given callback.
If I force the function to run after a post has been published earlier it returns the correct data. ie running manually.
$this->jackie_publish_post_hook('publish','draft',get_post(70801));
Is there a special way to access the first time submitted data from the hook callback function or what am I doing wrong?
Can I access the post meta from unsaved but submitted data to wordpress?