Using the state and city dynamic select fields example (https://github.com/Hube2/acf-dynamic-ajax-select-example/blob/master/dynamic-select-example/my-acf-extension.php), I have an events page (equivalent of city cpt) with a select field (event_type
) populated by rows (_event_type
) in a repeater (_event_types
) on an options page (equivalent of state cpt).
On Posts, I have a repeater (p_events
) with 2 select boxes in 2 columns:
| p_event_type
| p_event
|
I would like to populate p_event
with events from the events page depending on the selected event type on p_event_type
. p_event_type
is populated by rows from the repeater on the options page.
public function __construct() {
// event type field on event
add_action('acf/load_field/key=field_579376941cecc', array($this, 'load_event_type_field_choices'));
// event type field on post
add_action('acf/load_field/key=field_579376f522130', array($this, 'load_event_type_field_choices'));
// event field on post
add_action('acf/load_field/key=field_5793770922131', array($this, 'load_event_field_choices'));
...
}
public function load_event_type_field_choices($field) {
// this function dynamically loads the event type select field choices
// from the event types on options page repeater
...
}
public function load_event_field_choices($field) {
// this function dynamically loads event field choices
// based on the currently saved event type
// I only want to do this on Posts
global $post;
if (!$post || !isset($post->ID) || get_post_type($post->ID) != 'post') {
return $field;
}
// get the selected event type by row
$repeater = 'p_events';
$count = intval(get_post_meta($post->ID, $repeater, true));
for ($i=0; $i<$count; $i++) {
$event_type = get_post_meta($post->ID, $repeater.'_'.$i.'_'.'p_event_type', true);
$events = $this->get_events($event_type);
$field['choices'] = $events;
return $field;
}
}