Writing a function to send an email to the user on save_post if a taxonomy equals a specific value. However, I only want this to happen when the value is being changed, and not on every save. Is there a way to compare if a value is being updated with a new value versus the same value it already has?

 function status_save_email( $post_id ) {
  if ( !wp_is_post_revision($post_id) ) {
    $slug = 'sessions';
    if ( $slug != $_POST['post_type'] ) { return; }

    $status = get_the_terms( $post_id, 'status_tax' );
      if ( $status != null ){ 
      foreach( $status as $state ) {
        $state_name = $state->name ;
    }}

    if ( $state_name == 'Rejected' || $state_name == 'Approved' ){ 
        echo "bingo";
    }

  }
}

1 Answer
1

In the end I checked against and updated a new meta value on each save.

  $screen = get_current_screen();
  if ( $screen->base == 'post' && $screen->post_type == 'sessions') {
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    if ( isset( $_POST['session_status_tax'] ) ) {
      $status = $_POST['session_status_tax'];
    } else {
      $status="";
    }

    $prev_term = get_post_meta( $post_id, 'prev_term', 'true' );

    if ( $status === 'status-approved' &&  $status !== $prev_term ) {
      write_post_log($post_id, 'Approved' );
      send_email( $post_id, 'Approved' );
    }

    update_post_meta( $post_id, 'prev_term', $status );

 }
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *