Support

Account

Home Forums Front-end Issues ACF data not working with function using future_to_publish

Solved

ACF data not working with function using future_to_publish

  • I have a function that I use to send an email notification when a scheduled post gets published. The post is created using a front-end form.

    
    function post_scheduled_notification( $post_id )  {
    		
       	
        // A function to perform actions when a post is published
    	
    	$permalink = get_permalink($post_id);
    	$post_title = get_the_title( $post_id ); 
    	$post_image = ($_POST['acf']['field_55842cd3f9066']);
    	$to      = '[email protected]';
    	$subject = 'Your scheduled post has been published';
    	$headers = "MIME-Version: 1.0" . "\r\n";
    	$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    	$headers .= 'From: Daniel <[email protected]>' . "\r\n" .
            'Reply-To: [email protected]' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
    
    $message =  '<h1>Your scheduled post has published and is ready to be shared!</h1> 
    <a href="'. $permalink .'"><img style="border-radius:5px; width:365px;" src="' . $post_image . '" alt="'.$post_title.'" /></a>
    	
    mail($to, $subject, $message, $headers);
    }
    add_action( 'future_to_publish', 'post_scheduled_notification', 10, 3 );
    

    This code works perfectly when I use the action ‘acf/save_post’ however when I switch the action to ‘future_to_publish’ the post publishes correctly but does not have the ACF field data.

    Does anyone know what is the key difference between ‘acf/save_post’ and ‘future_to_publish’ that could be causing this issue? Or, how I can make sure the ACF data is saved before the future_to_publish function is run?

    Thanks

  • Hi @dhacree

    I believe the image is provided when a user scheduled a post. If that’s the case, it means the image is already saved in the database when the “future_to_publish” action executed. That means you can use get_field('the_field_name', $post_id) instead of getting the image using the $_POST variable.

    I hope this makes sense 🙂

  • Thanks for taking the time to respond! Seems pretty obvious now. Appreciate your help.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘ACF data not working with function using future_to_publish’ is closed to new replies.