enter image description here

I have a Custom Post Type named Task. I created a function that sends an email to the selected Agent, notifying a new task has been assigned. Here is the function:

function real_estate_send_mail_to_agent() {
    global $post;

    // If this is just a revision, don't send the email.
    if ( wp_is_post_revision( $post->ID ) ) {

    // Exit function if post type is not equal to task
    if ( $post->post_type !== 'task' ) {

    // Email header
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    // Recipient
    $agent = get_field_object("agent", $post->ID);// Get agent object from user_table
    $emailTo = (string) $agent['value']['user_email']; // Get agent email

    $agent_display_name = $agent['value']['display_name']; // Get agent display name

    // Email Subject
    $subject = "New Task: " .wp_strip_all_tags(get_the_title($post->ID));;

    // Email Body
    $message = "Hi <b>".$agent_display_name."</b><br/>"
    $message .= "You have been assigned a new task <br/>";
    $message .= "Please have a look at it ".get_permalink( $post->ID );

    // Send the mail
    wp_mail( $emailTo, $subject, $message, $headers );
add_action('save_post', 'real_estate_send_mail_to_agent', 11);

The function sends the email to the agent which is fine. The issue is that it triggers even when the post is updated or move to trash.

I want it to fire only when a new post is created using the save_post. The reason I need to use save_post is because I have to get the agent email from the user object in the User Field Type using the ACF plugin. If I use publish_post, the email is not sent, since it can't fetch the agent email. Help please.

  • I don't understand why you can not use publish_post action. Please explain why. "I can not do something" says nothing about the problem. If the user field type is stored in post meta, it should be available in publish_post action.
    – cybmeta
    Commented Mar 18, 2016 at 9:00
  • I can't use publish_post because when I trigger the mail function, it doesn't fetch the user_email from the user object at that time. I have to use save_post so that when it is saved, it can get the user_email
    – Nas Atchia
    Commented Mar 18, 2016 at 10:26
  • But why? I still don't understand and you still don't explain the reason. You can not get the data, why? Which error do you get? None, any, you don't know? Again, the post meta field, agent, should be available in publish_post just like it is in save_post. Maybe it is a problem with ACF, you should ask to them. Anyway, if you can use save_post, @userabuser is correct in his answer.
    – cybmeta
    Commented Mar 18, 2016 at 10:40
  • Yea this is something with ACF and I have to use the save_post. Thanks anyway :)
    – Nas Atchia
    Commented Mar 18, 2016 at 10:42
  • Possible duplicate of Check for update vs new post on save_post action Commented Mar 18, 2016 at 12:46

1 Answer 1


The save_post action also passes three parameters to your callback, one of which being $update which denotes whether the post being saved is an existing post or not.

 * Save post metadata when a post is saved.
 * @param int $post_id The post ID.
 * @param post $post The post object.
 * @param bool $update Whether this is an existing post being updated or not.
function save_post_callback( $post_id, $post, $update ) {

    if ( $update ) {

    //business logic...


add_action( 'save_post', 'save_post_callback', 10, 3 );


  • Can't get the function to work with this answer :(
    – Nas Atchia
    Commented Mar 18, 2016 at 7:53
  • In what manner? Wht does "can't get the function to work" mean? Have you debugged properly? E.g. ensure you are entering the callback and whether you are entering the conditional block or passing beyond it?
    – Adam
    Commented Mar 18, 2016 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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