Home › Forums › General Issues › Get ACF field values of related group (via Post Object) in functions.php
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?
// Get submitted values.
$values = $_POST['acf'];
$measurement_client = $_POST['acf']['field_5e147914518a6']; // Post Object from CLIENT group
$measurement_client_email_addresses = ?????? // Repeater field in 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 in CLIENT group
}
$to = implode(',', $list);
}
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?
See my last comment here https://support.advancedcustomfields.com/forums/topic/get-_postacf-value-of-repeater-field-in-post-object/#post-135228
$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');
}
}
John, again you’re a life safer.
Apparently I was searching in the wrong direction.
Many many tnx.
All kuddos go to you:
https://stackoverflow.com/questions/64222453/get-acf-field-values-of-related-group-via-post-object-in-functions-php/64246663#64246663
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.