0

I like to have a email send if a custom post is created/updated if it has custom taxonomy term.

The here is the CPT, taxonomy and term.

  • Custom Post Type: Call
  • Custom taxonomy: call_type
  • Custom Term: In progress

The code below work fine for just sending a mail based on the CTP.

////////////////////////////////////////////////////////////////////
//  Add Hooks for Email 
////////////////////////////////////////////////////////////////////
add_action('new_to_publish', 'send_emails_on_new_event');
add_action('post_updated', 'send_emails_on_new_event');
////////////////////////////////////////////////////////////////////
//  SET EMAIL FROM ADDRESS
////////////////////////////////////////////////////////////////////
function change_mail_from() {
    return "noreply@abc.com";
}
add_filter ("wp_mail_from", "change_mail_from");
////////////////////////////////////////////////////////////////////
//  SET EMAIL FROM NAME
////////////////////////////////////////////////////////////////////
function change_from_name() {
    return "ABC";
}
add_filter ("wp_mail_from_name", "change_from_name");
////////////////////////////////////////////////////////////////////
//  SET EMAIL TYPE TO HTML
////////////////////////////////////////////////////////////////////
function wpse27856_set_content_type(){
    return "text/html";
}
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
////////////////////////////////////////////////////////////////////
//  Send emails on event publication
////////////////////////////////////////////////////////////////////

function send_emails_on_new_event($post_id)
{
    global $post;
    $post           =   get_post($post_id);
    $post_id        =   $post->ID;
    $post_type      =   'call'; //post, page, attachment or whatever other CPT you may have
    $post_term      =   ''
    $author         =   get_userdata($post->post_author);
    $mf_area        =   $author->mf_area;
    $author_mail    =   $author->user_email;
    $author_name    =   $author->mf_d_name;
    $ac_name        =   get_post_meta( $post_id, '_call_8', true);
    $ac_email       =   get_post_meta( $post_id, '_call_9', true);
    $rc_email       =   get_post_meta( $post_id, '_call_12', true);
    $rc_name        =   get_post_meta( $post_id, '_call_13', true);
    $mtm_email      =   'support@abc.com';
    $emails         =   "$author_mail, $ac_email, $mtm_email"; //If you want to send to site administrator, use $emails = get_option('admin_email');
    $title          =   wp_strip_all_tags(get_the_title($post_id,));
    $url            =   home_url();
    ////////////////////////////////////////////////////////////////////
    //  Email lay out
    ////////////////////////////////////////////////////////////////////   
    ob_start(); ?>
    <html>
        <head>
            <style>
                a {
                    color: #ff3333;
                    font: 1.5rem;
                    line-height: 2rem;
                    text-decoration: none;
                    }
            </style>
        </head>
        <body>
            <p>
                Hi 
            </p>
            <p>
                Ref number <?php echo $title ;?> has been created.
            </p>

            <p>
                <strong>Details:</strong><br />
                <a href="<?php echo $url ;?>">Log in to track this call.</a><br/>
            </p>
            <p>
                Regards,<br />
                ABC
            </p>
        </body>
    </html>
    <?php
    $message = ob_get_contents();
    ob_end_clean();
    if(get_post_type($post) === $post_type)
    wp_mail($emails, "New Item listed on Fit It List. Ref number $title", $message);
}
////////////////////////////////////////////////////////////////////
//  End Fire mail on post page and CPT
////////////////////////////////////////////////////////////////////

Can someone please help to make the script work for terms as well.

0

Have you tried using has_term()?

if( get_post_type($post) === $post_type && has_term( 'In progress', 'call_type', $post ) )
    wp_mail($emails, "New Item listed on Fit It List. Ref number $title", $message);
  • Thank you working fine now. – MFWebMaster Nov 16 at 9:03
  • Glad to hear that. If this helped you solve it, marking it as the accepted answer would be appreciated. – butlerblog Nov 16 at 12:44

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.