0

I would like to use the get_post_meta to pull a value from a CPT dynamically and then send a email.

The problem is that the get_post_meta($post->ID, ... part of the code does not pull the email from the CPT metabox. If you replace the $post->ID, with a post ID example "254" the entire function works perfectly. Is there a way that I can have get_post_meta use the post ID dynamically

    <?php
    ////////////////////////////////////////////////////////////////////
    // Fire mail on post page and CPT
    ////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////
    //  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@somedomain.co.za";
    }
        add_filter ("wp_mail_from", "change_mail_from");

    ////////////////////////////////////////////////////////////////////
    //  SET EMAIL FROM NAME
    ////////////////////////////////////////////////////////////////////
    function change_from_name() {
        return "Some Where";
    }
    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)
    {
        $post                   =   get_post($post_id);
        $post_id                =   $post->ID;
        $post_type              =   'CPT name'; //post, page, attachment or         
    whatever other CPT you may have
        $author                 =   get_userdata($post->post_author);
        $author_id              =   $author->ID;
        $extra_email            =   get_post_meta($post->ID,     '_metabox_flied_from_CPT_with_extra_email', true);
        $admin_email            =   get_option('admin_email');
    $emails                 =   "$admin_email, $extra_email, $author->user_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                    =   get_permalink($post->ID);
    ////////////////////////////////////////////////////////////////////
    //  Email lay out
    ////////////////////////////////////////////////////////////////////   
       ob_start(); ?>
        <html>
                <head>
                <title></title>
            </head>
            <body>
            <p>
                Hi 
            </p>
            <p>
                <?php echo $author->user_login ;?> has created a new entry maintenance system.
            </p>
            <p>
                Some Content
            </p>
        </body>
    </html>
    <?php
    $message = ob_get_contents();
    ob_end_clean();
    if(get_post_type($post_id) === $post_type)
    wp_mail($emails, "New/Updated Post. Ref number $title", $message);
}?>

Notes:

The funny thing is if you update a post all work well.

add_action('post_updated', 'send_emails_on_new_event');
0

You can try to add global $post; on top of your code. But it depends on where the function is called.

  • require get_template_directory() . '/inc/function-send-email-if-maintenances-post.php'; in the functions.php – MFWebMaster Oct 10 '18 at 13:07
  • Your code doesn't make sense. There is a function with an argument $post_id, but inside that function you retrieve the $post->ID again? – Pim Oct 10 '18 at 13:15
  • Thank will fix but $post_id = $post->ID so does not matter with one you use – MFWebMaster Oct 10 '18 at 13:32
  • If you call this function, you already have the post's id, don't you? So why do you have to search for it again inside the function? Doesn't make the least bit of sense to me... – Pim Oct 10 '18 at 14:14
  • Even if I change the $post->ID to $post_id the script still does not work. – MFWebMaster Oct 10 '18 at 14:28

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.