Skip to content

Instantly share code, notes, and snippets.

@leepettijohn
Last active October 25, 2019 21:52
Show Gist options
  • Save leepettijohn/713e62da3cb419366503a78ceb7b4dc8 to your computer and use it in GitHub Desktop.
Save leepettijohn/713e62da3cb419366503a78ceb7b4dc8 to your computer and use it in GitHub Desktop.
ACF - Populate Dropdown in Repeater Field - with Post Name and ID
<?php
/*
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/#example-2
*** CHANGE ***
- child_field (this is the dropdown inside the repeater field)
- post_slug
*/
function acf_pre_populate_repeater_dropdown ( $field ) {
$field['choices'] = array();
$args = array('post_type' => 'post_slug');
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$value = get_the_ID();
$label = get_the_title();
$field['choices'][ $value ] = $label;
}
} else {}
wp_reset_postdata();
return $field;
}
add_filter('acf/load_field/name=child_field', 'acf_pre_populate_repeater_dropdown');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment