I have a site where people can submit drafts to a custom post type from the frontend.

Once a user submits the form, a new WP user is created.

If the user is logged in the submitted user e-mail adress is already attached to a WP user, the new post draft will be assigned to that author.

So now I want to setup a function that sends two different emails to the author this when admin changed post status from draft to published.

  1. If user has published posts in the custom post type already
  2. If user does not have any published posts in the custom post type but a draft

Current function (not working)

/** Notification - Draft goes online */
    function authorNotification($post_id) {
        global $wpdb;
        $post = get_post($post_id);
        if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {

            $author_info = get_userdata($post->post_author);
            $author_id = $author_info->ID;

            $published_by_author = new WP_Query(array(
               'post_type' => 'my-custom-post-type', 
               'post_status' => array('published','pending'), 
               'author' => $author_id, 
               'nopaging' => true
            if( $published_by_author->found_posts > 0 ) { // user has published posts
               // wp_mail something like "another post of yours is online"
            } else { // user does not have published posts
              // wp_mail something like "welcome, your first post is online"

    add_action('publish_my-custom-post-type', 'authorNotification');


When I submit my frontend form, a new user is created and the form contents are saved as a post draft. Fine so far.

But when the draft is published – and the author does not have any published posts yet – the first email saying "another post of yours is online" is being sent.

When submitting the form while being logged in, the same email is being sent again.

Realizations on second thought...

Of course the post count will return 1 even if the user is new because the wp_mails are sent when their first post goes from draft to published. Duh.

I'm going to edit my code and post it here later.

  • What is the exact problem, a hook is not being called OR mail function is not working? – WisdmLabs Jun 25 '15 at 11:15
  • 1
    edited my question and noticed I'm stupid. of course the post count is at least 1 for the new user because the draft is published and THEN the mail is sent. duh. – okiedokey Jun 25 '15 at 11:53
  • 1
    Please post a proper answer, with your corrected code. – s_ha_dum Jun 25 '15 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.