Get ACF field values of related group (via Post Object) in functions.php

2.1k views Asked by At

I have 2 WordPress custom post types: CLIENT and MEASUREMENT

I also have 2 ACF groups: CLIENT and MEASUREMENT

I have multiple CLIENT posts.

When I create a new MEASUREMENT post I can select a CLIENT via a 'Post object field' to set the relation with the CLIENT.

When I am done with the MEASUREMENT post I want to email the data to the CLIENT.

I know how to send an email with all MEASUREMENT data but I can't get the email addresses (repeater field) from within the related CLIENT post.

When I call these CLIENT email addresses within a template file I can do something like this (working):

$measurement_client                 = get_field('measurement_client'); // Post Object in MEASUREMENT group
$measurement_client_email_addresses = get_field('company_email_addresses', $measurement_client->ID); // Repeater field in CLIENT group

but I figured out I can't use this code in my functions.php file because I have to use $_POST['acf'] in stead of get_field and here's where I lose it.

The code below is working (tested it with a demo $to) but I don't know what needs to be placed on ?????? to get the sub field values of the related field within the related CLIENT post...

I hope my question is clear?

// Send measurement report emails
add_action('acf/save_post', 'yl_send_measurement_report_emails', 5);
function yl_send_measurement_report_emails( $post_id ) {

    // Get submitted values.
    $values             = $_POST['acf'];

    $measurement_client                     = $_POST['acf']['field_5e147914518a6']; // Post Object from CLIENT group
    $measurement_client_email_addresses     = ?????? // Repeater field of CLIENT group

    if ( $measurement_client_email_addresses ) {
        $list = array();
        foreach( $measurement_client_email_addresses as $measurement_client_email_address ) {
            $list[] = $measurement_client_email_address['field_5e1452c41945c']; // Sub field of the Repeater field in the CLIENT group
        }
        $to = implode(',', $list);
    }


    $subject    = get_field('measurement_setting_email_subject', 'measurement_settings');
    $message    = get_field('measurement_setting_email_content', 'measurement_settings');

    $headers = array
        (
        'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
        'X-Mailer: PHP/' . phpversion(),
        'MIME-Version: 1.0',
        'Content-type: text/html; charset=iso-8859-1'
    );
    $headers = implode( "\r\n" , $headers );

    wp_mail( $to, $subject, $message, $headers );

}

So in other simple words: How do I retrieve a field value (in functions.php) of a field that is not actually in the GROUP/POST itself but in a connected (via post object) GROUP/POST?

1

There are 1 answers

0
Yorlinq On BEST ANSWER

Just to share:

$measurement_client_id = intval($_POST['acf']['field_5e147914518a6']);
$emails = array();

if (have_rows('email_repeater', $measurement_client_id)) {
  while (have_rows('email_repeater', $measurement_client_id)) {
    the_row();
    $emails[] = get_sub_field('email_field');
  }
}

All kuddos go to John Huebner.