1

I have a custom post type immobile with some custom fields created with Toolset Types.

I need to send and email with the title and a custom field when a new immobile is saved as a draft or it's published.

I managed to do almost everthing. My code is like this:

add_action( 'auto-draft_to_draft', 'my_send_email', 99 );
add_action( 'auto-draft_to_publish', 'my_send_email', 99 );
add_action( 'draft_to_publish', 'my_send_email', 99 );
function my_send_email( $post ) {
  if ( $post->post_type == 'immobile' ) {
    wp_mail(
      'email@example.com',
      'New immobile saved as ' . $post->post_status,
      'Title: ' . $post->post_title . '. Ref: ' . get_post_meta( $post->ID, 'wpcf-reference', true ),
      array('Content-Type: text/html; charset=UTF-8')
    );
  }
}

Unfortunately my custom field is not showing in the email, probably because It get saved after the hook I'm using.

The perfect solution would be a hook wich:

  1. Is fired after the post meta are saved
  2. Allow me to check the post_status of the post before it was saved.

I tried the hook post updated but I got the same result: the custom field does not show up.

Which hook could I use? Or I should try a totally different approach?

3
  • 1
    Not familiar with toolset , it might be because it saves the value after post is published . Does it have separate action like acf/save_post ?
    – Anoop D
    Jun 11 '20 at 16:01
  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. Jun 11 '20 at 16:01
  • @AnoopD unfortunately their documentation says "Types does not have a proper hooks API". However, this applies to all custom fields, not only the ones created with Toolset
    – Pacicio
    Jun 11 '20 at 16:38
0

I think you need to handle post request first. You have to do this because gutenberg calls the hook twice.

function transition_post_status_handler( $new_status, $old_status, $post ) {
    if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
        send_mail_on_publish( $new_status, $old_status, $post );
        set_transient( 'post_status_updater_flag', 'done', 10 );
    } else {
        if ( false === get_transient( 'post_status_updater_flag' ) )
            send_mail_on_publish( $new_status, $old_status, $post );
    }
}
add_action( 'transition_post_status', 'transition_post_status_handler', 10, 3 );

Then you can call the function to send the email. At this point I think you have the data of the meta available.

function send_mail_on_publish( $new_status, $old_status, $post ) {

    if ( 'publish' == $new_status && 'immobile' == get_post_type($post) ) {

        // Check if not an autosave
        if ( wp_is_post_autosave( $post->ID ) )
            return;

        // Check if not a revision
        if ( wp_is_post_revision( $post->ID ) )
            return;

        // Send email

    }
}
5
  • At the moment my client doesn't want to use Gutenberg and it's sticking to the classic editor. I'll give it a try outside her working hours. If it works as expected, this could be the reason for her to finally switch to Gutenberg
    – Pacicio
    Jun 12 '20 at 14:02
  • I haven't tested the script on the classic editor but I think it works with him too. You just have to test it.
    – leitmotiv
    Jun 12 '20 at 14:10
  • Nope, it doesn't send any email. I tried removing the if ( defined( 'REST_REQUEST' ) ... and I get 10 emails in my inbox! 8 without custom field, 2 with it. This gives me hope for Gutenberg
    – Pacicio
    Jun 12 '20 at 16:20
  • I just changed the transition_post_status_handler function try now
    – leitmotiv
    Jun 12 '20 at 17:24
  • I tried with Gutenberg and the result is similar: 7 emails instead of 10, 2 with the custom field. But I think you function is a good base to work with, I just need some time to do some more testing.
    – Pacicio
    Jun 15 '20 at 15:00

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.