php – How to auto send email when publishing a custom post type using acf field values

Question

I would like to automatically send out an email when a custom post type gets published using the ACF field values. I go through the solution here. but this needs some additional codes for my cause. This is my code snippet

    //Send Email
    function send_mails_on_publish( $new_status, $old_status, $post )
        {
        if ( 'publish' !== $new_status or 'publish' === $old_status
            or 'praelegal-enquires' !== get_post_type( $post ) )
            return;
        
        $subscribers = get_users( array ( 'role' => 'subscriber' ) );
        $emails = array ();
        
        foreach ( $subscribers as $subscriber )
            $emails[] = $subscriber->user_email;
        print_r(get_post_custom($post->ID));
        echo "Testing Purpose $meme";
        
        
        foreach ( $subscribers as $subscriber )
            $emails[] = $subscriber->user_email;

        $body = sprintf( 'Hey there is a new entry!
            See <%s>',
            get_permalink( $post )
            );


        wp_mail( $emails, 'New entry!', $body );
        }
    add_action( 'transition_post_status', 'send_mails_on_publish', 10, 3 );
    //End Send Email

I have a contact form that posts to a custom post type. I want to send an email to the user’s email that fills the contact form. I can access the Post itself using the $post variable, but I can not access the email ACF field value from that post. this is the custom post type:
{
“id”: 300,
“date”: “2021-12-06T15:25:51”,
“date_gmt”: “2021-12-06T10:55:51”,
“guid”: {
“rendered”: “http://wpbackend.navisa.af/praelegal-enquires/asking-for-praelegal-services42/”
},
“modified”: “2021-12-06T15:25:51”,
“modified_gmt”: “2021-12-06T10:55:51”,
“slug”: “asking-for-praelegal-services42”,
“status”: “publish”,
“type”: “praelegal-enquires”,
“link”: “http://wpbackend.navisa.af/praelegal-enquires/asking-for-praelegal-services42/”,
“title”: {
“rendered”: “Asking for Praelegal Services42”
},
“content”: {
“rendered”: “”,
“protected”: false
},
“featured_media”: 0,
“template”: “”,
“acf”: {
“name”: “emal”,
“email”: “yesrebosmani@gmail.com”,
“message”: “hi there!”
},
“_links”: {
“self”: [
{
“href”: “http://wpbackend.navisa.af/wp-json/wp/v2/praelegal-enquires/300”
}
],
“collection”: [
{
“href”: “http://wpbackend.navisa.af/wp-json/wp/v2/praelegal-enquires”
}
],
“about”: [
{
“href”: “http://wpbackend.navisa.af/wp-json/wp/v2/types/praelegal-enquires”
}
],
“wp:attachment”: [
{
“href”: “http://wpbackend.navisa.af/wp-json/wp/v2/media?parent=300”
}
],
“curies”: [
{
“name”: “wp”,
“href”: “https://api.w.org/{rel}”,
“templated”: true
}
]
}
}
I would like to get access to the ACF email field and send out an email.

0
Emal Nawabi 1 month 2021-12-06T07:12:15-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse