Home › Forums › Add-ons › Repeater Field › Populate select field with values from repeater field › Reply To: Populate select field with values from repeater field
Hi @davidjustin
This will be possible, but lets first forget about the AJAX side. Lets make it a bit more basic:
1. The user selects a post type
2. The user saves the post
3. The user sees the select field populated with repeater values from the post type selected
<?php
function my_acf_load_field( $field )
{
// globals
global $post;
// load selected value from post type field
$p = get_field( 'Field_B', $post->ID );
// now load repeater field from selected post object
if(get_field( 'Field_B', $p->ID ))
{
// loop through the repeater and use the sub fields "value" and "label"
while(has_sub_field('Field_B', $p->ID))
{
$value = get_sub_field('value');
$label = get_sub_field('label');
$field['choices'][ $value ] = $label;
}
}
return $field;
}
// acf/load_field/name={$field_name} - filter for a specific field based on it's name
add_filter('acf/load_field/name=Field_A', 'my_acf_load_field');
?>
This is untested but should work. After you have it working, you can write some AJAX to fetch the values.
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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.