3

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";
    }

  }
}
3

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 );

 }
}

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.