I need to send an email to the admin user when a post of the post type "task" is created. Any help would be really appreciated. Thanks!

3 Answers 3


If you want to send an email on first publish and on updates, you can avoid many extra checks by using {status}_{post type} action hook. Put the code below into the current theme's functions.php:

add_action( 'publish_task', 'wpse_admin_email', 10, 2 );
function wpse_admin_email( $post_id, $post ) {
    // prepare and send email code goes here...

If you want to send an email on first publish only, use the code below:

add_action( 'transition_post_status', 'wpse_admin_email_once', 10, 3 );
function wpse_admin_email_once( $new, $old, $post ) {
    if ( $post->post_type == 'task' && $new == 'publish' && $old == 'auto-draft' ) {
        // prepare and send email code goes here...

You can add action after wp insert post with specified post type:

    function after_task_post_created( $post_id ) {
    //no action if post type not task
    if (get_post_type($post_id) != 'task')
    // If this is a revision, don't send the email.
    if ( wp_is_post_revision( $post_id ) )

   // if post not yet published so no action taken, i know it can be confused
   if (get_post_status( $post_id ) != 'publish' )

    // your email action

        $yoursubject = 'This is your email subject';

        $yourmessage = "a body message here";

        // Send email.
    $adminemail = get_option( 'admin_email' );
        wp_mail( $adminemail, $yoursubject, $yourmessage );
    add_action( 'wp_insert_post', 'after_task_post_created');






You could use this very easy action to control more than the creation of the custom post_type

add_action( 'transition_post_status', 'transition_fun', 10, 3 );
function transition_fun( $new, $old, $post ) {
    if ( ( $new == 'publish' ) && ( $old != 'publish' || $old != 'auto-draft' ) && ( $post->post_type == 'task' ) ) {
        echo 'send mail......';
    } else {

Just insert that to your functions.php or as a plugin.

If for example later you want to have access to the draft status you could easily do that with the 'auto-draft'

  • Your if statement includes wrong check for the post type. It should check for 'task', not for 'post'. Sep 15, 2017 at 18:44
  • @FrankP.Walentynowicz Nice catch! Sep 16, 2017 at 18:58

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.