-1

I have two post type named ‘post-type-one’ and ‘post-type-two.' Both post type has exact similar category a,b,c,d,e I want when a post publish under category ‘a’ on ‘post-type-one' send a email notification to an author under category ‘a’ on ‘post-type-two’. Can anyone suggest me a plugin (free or premium) to do this? Or do you have any idea how it can be done? Thanks in advance :)

  • Hi, do you have the email address of the author or you want your code to find it automatically or something? – Jack Johansson Feb 8 '17 at 14:00
0

You can use this code to automatically send an email whenever a post of the type one is published. This goes into your theme's functions.php :

<?php 
    function post_published_notification( $ID, $post ) {
        $type = get_post_type($ID);
        if ($type == 'post-type-one'){
            $author = $post->post_author; /* Post author ID. */
            $name = get_the_author_meta( 'display_name', $author );
            $email = get_the_author_meta( 'user_email', $author );
            $title = $post->post_title;
            $permalink = get_permalink( $ID );
            $edit = get_edit_post_link( $ID, '' );
            $to[] = sprintf( '%s <%s>', $name, $email );
            $subject = sprintf( 'Published: %s', $title );
            $message = sprintf ('%s, Your article “%s” has been published.' . "\n\n", $name, $title );
            $message .= sprintf( 'View: %s', $permalink );
            $headers[] = '';
            wp_mail( $to, $subject, $message, $headers );
        }
    }
    add_action( 'publish_post', 'post_published_notification', 10, 2 );
?>

You can read and the code and modify the parts that you want, depending on your needs. I could post a more accurate answer if your question was clear. However, the function itself is pretty explanatory.

  • 1
    Hi Jack Johansson, i appreciate your answer :) i am trying to make it more clear now. i have two post type 'health' another 'customer' and two user role 'health' and 'customer' where 'health' user can post a post under 'health' post type and 'customer' user can post a post under 'customer' post type. Both post type have same category a,b,c,d so i want send a email notification when 'customer' user publish a post under 'customer' post type with category 'a', the email notification will go 'health' user/ author who previously published a post under 'health' post type with 'a' category. – rasel mahmud Feb 9 '17 at 12:28
  • Does it make more clear than previous details? if not let me know i will try to present it another way :) – rasel mahmud Feb 9 '17 at 12:30

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.