Support

Account

Home Forums ACF PRO Create multiple posts with ACF/pre_save_post

Unread

Create multiple posts with ACF/pre_save_post

  • Hi,

    I have an ACF Form, in which users send their CV details.
    each time the form is submitted, a new post (under a CPT) is created and saved with the form details and a custom email being sent to me through the acf/save_post action.

    I want to let users to apply to multiple jobs at once, so I have a field with the number of jobs the user is applying for (and their IDs- so I can send it to the correct details).

    Is there a way to loop through these IDs and trigger the new post and email by the jobs count?

    function my_acf_save_post( $post_id ) {
        // bail early if not a sent_resume post
        if( get_post_type($post_id) !== 'sent_resume' ) {
            return;
        }
        // bail early if editing in admin
        if( is_admin() ) {
            return;
        }
    
        // vars
        $post = get_post( $post_id );
        //$job_count = get_field('job_multiple', $post_id);
    
            function wpdocs_set_html_mail_content_type() {
                return 'text/html';
            }
            add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    
            // get custom fields (field group exists for content_form)
            $name = get_field('sender_name', $post_id);
            $email = get_field('sender_email', $post_id);
            $content = get_field('job_message', $post_id);
            $attached_file = get_field('resume_file', $post_id);
            $job_id = get_field('job_id', $post_id);
            $sendTo = get_field('job_email', $job_id->ID);
    
            if(!$sendTo){
                $adv_company = get_field('adv_company', $job_id->ID);
                $sendTo = get_field('default_jobs_email', $adv_company->ID);
            }
    
            // email data
            $multiple_recipients = array($sendTo);
    
            $attachmentPath = explode('/wp-content', $attached_file);
            $attachmentPath = $attachmentPath[1];
            $attachment = array(WP_CONTENT_DIR.$attachmentPath);
            $headers[] = 'From: mysite <mysite@mysite.org>' . "\r\n";
    
            $subject = $post->post_title;
            $body = '';
            $body .= '<html>';
            $body .= '<body style="direction: rtl; text-align: right;">';
            $body .= '<div class="content">'.$post->post_title.'</div></br/>';
            $body .= 'name: '.$name.'</br/>';
            $body .= 'email: '.$email.'</br/>';
            $body .= '<div class="content">'.$content.'</div>';
            $body .= '<div class="content">'.$sendTo.'</div>';
            $body .= '</body>';
            $body .= '</html>';
    
            // send email
            wp_mail($multiple_recipients, $subject, $body, $headers, $attachment );
            remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    
        //wp_redirect(get_permalink($post_id));
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
    function my_pre_save_post( $post_id ) {
    
        // check if this is to be a new post
        if( $post_id != 'new' ) {
            return $post_id;
        }
    
        // Create a new post
        $post = array(
            'post_status'  => 'draft' ,
            'post_type'  => 'sent_resume' ,
        );
    
        // insert the post
        $post_id = wp_insert_post( $post );
    
        // return the new ID
        return $post_id;
    
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.