Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zackpyle/444de4ab1b2255ef279e8330870dcbc2 to your computer and use it in GitHub Desktop.
Save zackpyle/444de4ab1b2255ef279e8330870dcbc2 to your computer and use it in GitHub Desktop.
Set the value of a Fluent Form radio field with an ACF Relationship field from the current page
<?php
add_filter('fluentform/rendering_field_data_input_radio', function ($data, $form) {
if ($form->id != 3) { // Form ID
return $data;
}
// Set which field to do this on
if (\FluentForm\Framework\Helpers\ArrayHelper::get($data, 'attributes.name') != 'FF_FORM_FIELD_NAME') { // Form field name here
return $data;
}
// Get current post ID
$post_id = get_the_ID();
// Get the related post from ACF relationship field
$options = [];
if (function_exists('get_field')) {
$related_posts = get_field('RELATED_POST_FIELD_NAME', $post_id); // Your ACF relationship field here
if (!empty($related_posts)) {
foreach ($related_posts as $related_post) {
$options[] = [
'label' => get_the_title($related_post),
//'value' => $related_post->ID // if you want the id of the post as the value
'value' => get_the_title($related_post) // if you want the title of the post as the value
];
}
}
}
// Clear existing options before adding new ones
$data['settings']['advanced_options'] = [];
// Add new options
if (!empty($options)) {
$data['settings']['advanced_options'] = $options;
}
return $data;
}, 10, 2);
@zackpyle
Copy link
Author

zackpyle commented Jun 5, 2024

Lets you add the values from the current post's ACF relationship as radio button values.

My use case was mortgage company and the loan officers have multiple offices (ACF relationship to the Office post type).

And the form is a Schedule Appointment form where they can select which office they want to meet at.

So it's the same form for every loan officer, but pulls in that specific LO's office locations they serve. (and uses a FF Smart Code {embed_post.acf.ACF_KEY} to send to the correct person too)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment