Support

Account

Home Forums General Issues Checking post status

Solved

Checking post status

  • Hi, I have a custom post type that uses ACF fields and the posts are created on admin dashboard side only by admins and using a user select field that assigns this post to user in the database.

    The users when logged in only see a list of the post assigned to them and can update them from the posts only from the front end of the site.

    The above all works fine and as I have intended it to but the problem is when a new post is created it should only send the user that has been assigned the post an email to state a post was assigned to them this does not work using the default WordPress code as it seems the post does not contain the settings on draft to publish and does not get any field details.

    When the user asigned the post edits and updates the post it sends emails to all admins this works correctly (published to published ) an the email contains the fields settings pulled to insert into body.

    How can I make sure that a new custom post type can grab the fields to send only to the user assigned.

    My code is based on wordpress update post code se shown below but if there is a better way to do this I will try to convert it using an ACF hook.

    Thanks! 🙂

    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, don't update it.
      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_activity = get_field( "activity" ,$post_id);
         $value_building = get_field( "building",$post_id );
         $value_start_date = get_field( "start_date",$post_id );
         $value_end_date = get_field( "target_end_date",$post_id );
         $value_completed_date = get_field( "completed_date",$post_id );
         $value_person = get_field( "responsible_person",$post_id );
         $value_status = get_field( "status",$post_id );
         $value_updated = get_field( "update",$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;
         
    
        //mailpostinfo($post_id);
        //////////////////////////////////////////////////////////////////
        $to = $user_email;
        $subject = 'A energy task for  '.$user_email.'  '.$post_id.' has been assigned to you';
        $body = 'You have been assigned a energy task due by: '.$value_date.' ';
        $body .= 'you can view it on the following link but will need to sign in first if not already signed in. '.$value_post_link;
        $body .= ' Please do not reply directly to this email as it is not monitored.';
        wp_mail( $to, $subject, $body);
        ///////////////////////////////////////////////////////////////////
    
      }
    
      // run codes only when a update is made to the task
      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_activity = get_field( "activity" );
         $value_building = get_field( "building" );
         $value_start_date = get_field( "start_date" );
         $value_end_date = get_field( "target_end_date" );
         $value_completed_date = get_field( "completed_date" );
         $value_person = get_field( "responsible_person" );
         $value_status = get_field( "status" );
         $value_updated = get_field( "update" );
         // link to the post
         $value_post_link = get_permalink( $post_id );
      
    if($value_status != 'Not Started'){
       
       $args = array(
            'role' => 'cm',
            'orderby' => 'user_nicename',
            'order' => 'ASC'
            );
            $users = get_users($args);
             // send to cm user role
             foreach ( $users as $user ) 
             {
               //////////////////////////////////////////////////////////////////
               $to = $user->user_email;
               $subject = 'energy task has been update for '.$value_building;
               $body = 'Energy task for '.$value_building.' was updated by '.$current_user->display_name.' to the status of: '.$value_status.'.';
               $body .= ' you can view it on the following link but will need to sign in first if not already signed in. '.$value_post_link;
               $body .= ' Please do not reply directly to this email as it is not monitored.';
               wp_mail( $to, $subject, $body);
               ///////////////////////////////////////////////////////////////////
             }
              
             $args2 = array(
            'role' => 'administrator',
            'orderby' => 'user_nicename',
            'order' => 'ASC'
            );
            $users2 = get_users($args2);
             // send for admins role
             foreach ( $users2 as $user ) 
             {
               //////////////////////////////////////////////////////////////////
               $to = $user->user_email;
               $subject = 'energy task has been update for '.$value_building;
               $body = 'Energy task for '.$value_building.' was updated by '.$current_user->display_name.' to the status of: '.$value_status.'.';
               $body .= ' you can view it on the following link but will need to sign in first if not already signed in. '.$value_post_link;
               $body .= ' Please do not reply directly to this email as it is not monitored.';
               wp_mail( $to, $subject, $body);
               ///////////////////////////////////////////////////////////////////
             }
    }
      
      }
    
    }
    add_action( 'post_updated', 'updatepostsz', 10, 3 );
  • When adding a new post, or when updating an existing post, the post_updated hook fires before ACF saves any of the current values in the fields to the database.

    On a new post you are not getting the values because they do not exist yet.

    On an existing post, while you are getting the values you are more than likely getting the old values and not getting the correct value for anything that has been changed.

    In order to ensure that you’re getting the latest values you need to rework you action/filter to use the acf/save_post hook detailed here https://www.advancedcustomfields.com/resources/acf-save_post/ and use a priority of > 10.

    You can continue using your filter, but instead of using get_field() to get any values you will need to use the $_POST[] values being submitted, there is information on using these on the acf/save_post page.

  • The above did not work for me

    Changed:
    $value_activity = get_field( “activity” );
    to:
    $field = $_POST[‘acf’][‘activity’];

    Still did not get the field value and not sure I can change to my_acf_save_post( $post_id ) as I need to know if it is a new created post or if the post is updated.

  • You will need to use the field keys and not the field names. Example: $POST['acf']['field_abc124'] rather than $_POST['acf']['activity']

  • John your a legend :-)….

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

The topic ‘Checking post status’ is closed to new replies.