1

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. The POST Request parameters from the browser side

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?

1 Answer 1

1

I usually handle this with save_post action.

// We'll use 1000, low priority (we'll let other hooks to run first)
add_action( 'save_post', [$this, 'jackie_publish_post_hook'], 1000, 3 ); 

Then within your class:

function jackie_publish_post_hook( $post_id, $post, $update ) {
    if ( $update ) return false; // Post already existed, don't proceed

    // Do stuff
}

Or use a post meta:

function jackie_publish_post_hook( $post_id, $post, $update ) {
    if ( get_post_meta( $post_id, 'jackie_post_processed', true ) != '' ) return false;
    update_post_meta( $post_id, 'jackie_post_processed', 1 );

    // Do stuff
}
3
  • Shouldn't it be if( $update ) return false;
    – domii
    Commented Apr 5, 2021 at 8:09
  • Also the update is return true even on first publish so I do not think this is the way to go.
    – domii
    Commented Apr 5, 2021 at 9:12
  • @domii you're right, it's if ( $update ) return false;. I have noticed that save_posts is triggered several times when you create a new post, however for the very first trigger it's guaranteed that $update = false. Commented Apr 5, 2021 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.