0

I am trying to send an email when a custom post type is published for the first time only as it assigns that post to a person and requires the email to be sent to just that user. The user is selected from a ACF select box that lists certain user roles.

After the user has logged in they can see only the posts assigned to them and they can fill in the rest of the custom fields once they save the post then an email is sent to all admins to state the user has updated the post. This is done by checking if old state is published and new state is published.

The user assigned can only update the posts on the front end but admins create them via the dashboard and can edit them on the front end or in the dashboard.

I have all of the functions working except the send to the person selected part in the custom filed set by ACF filled in post the fields don't seem to be populated until the post is saved when using add_action( 'post_updated', 'updatepostsz', 10, 3 ); and this does not allow me use the ACF populated information only when updating a post that was published already.

If my code looks wrong it is because I have stripped down to reduce clutter for helping with the issue.

My code is:

    function updatepostsz( $post_id, $post_after, $post_before ) {

  // Grab our post status
  $post_status = get_post_status($post_id);
  // get post type for checking
  $post_type = get_post_type( $post_id );
  // Before and after
  $post_after_email_var  = $post_after->post_status;
  $post_before_email_var = $post_before->post_status;



  // Stop anything from happening if revision or where we dont need it to be doing things
  if ( wp_is_post_revision( $post_id ) ) { return; }


  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
  // unhook this function so it doesn't loop infinitely
  remove_action( 'save_post', 'updateposts' );

  // If this isn't a custom post, run code.
  if ( "task" != $post_type ) return;


  // run codes on creation of new task only to email asigned person
  if (  $post_before_email_var != 'publish' &&  $post_after_email_var  == 'publish' ) 
  {


     // get user running script
     $current_user = wp_get_current_user();
     // the current users name
     $cu = $current_user->user_login;

     // all our information of custom post
     $value_person = get_field( "responsible_person",$post_id );
     // link to the post
     $value_post_link = get_permalink( $post_id );
     //grabs the id of the person assigned to
     $select_staff_id = $value_personcu['ID'];
     // use our id to grab details on the user
     $author_obj = get_user_by('id', $select_staff_id);
     $user_email = $author_obj->user_email;


    //does not work in getting fields!!!!!!!!!!!!!!!
    // Fields not saved until after this hook so email is empty
    //////////////////////////////////////////////////////////////////
    $to = $user_email;
    $subject = 'A user email '.$user_email.'  '.$post_id.' has been assigned to you';
    $body = 'You have been assigned a post task due by: '.$value_date.' ';
    wp_mail( $to, $subject, $body);
    ///////////////////////////////////////////////////////////////////

  }


  // run codes only when a update is made to the task and works!
  if ( $post_before_email_var == 'publish' && $post_after_email_var  == 'publish' )
  {
     // get user running script
     $current_user = wp_get_current_user();
     // the current users name
     $cu = $current_user->user_login;

     // all our information of custom post
     $value_person = get_field( "responsible_person" );
     // link to the post
     $value_post_link = get_permalink( $post_id );

           //////////////////////////////////////////////////////////////////
           $to = $user->user_email;
           $subject = 'post updated by user';
           $body = 'body message';
           wp_mail( $to, $subject, $body);
           ///////////////////////////////////////////////////////////////////

  }

}
add_action( 'post_updated', 'updatepostsz', 10, 3 );

Thanks to anyone that can solve this as I have googled and tried various save post actions all have not worked.

0

I used the save_post_CPT() hook for doing this.

I can't remember if it sent the content the first time though:

try this code:

function my_project_rt_send_email( $post_id ) {

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
// If this is just a revision, don't send the email.
//if ( wp_is_post_revision( $post_id ) )
//  return;

// only go if user checked media submission box.

if(isset($_POST['media-submit'])){

    $post_title = get_the_title( $post_id );
    $post_url = get_permalink( $post_id );
    $post_content = get_the_content( $post_id );

    $content_post = get_post( $post_id );
    $content = $content_post->post_content;

    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);


    $subject = 'Important - A NEW POST FOR YOU';
    $headers = array('Content-Type: text/html; charset=UTF-8');
    $headers[] = 'From: YOUR SITE <your@email.com>';

    // COPY USER BASED ON FIELD SEND TO USER BASED ON FIELD
    if( get_field('distribution_email') )
        $headers[] = 'Bcc: '. get_field('distribution_email');

    $message = "<h3>".$post_title."</h3>";

    $message .= $content;
    $message .= '<a href="'. $post_url.'">'.$post_title.'</a>';

    // Send email to admin.
    wp_mail( 'info@example.com', $subject, $message, $headers);
    }
}
add_action( 'save_post_my_cpt', 'my_project_rt_send_email' );

You'll want to change the "save_post_MYCPT" to your cpt name, change the to email, and the from in the first header array addition.

I don't know if your to field is a select or if text field, in this case it's a text, so you may need to change that as well.

Also I had a media check box in mine so maybe remove the

if(isset($_POST['media-submit'])){

and the attached its attached closing bracket "}" too since you need them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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