Home › Forums › General Issues › a new select field with data from query_args › Reply To: a new select field with data from query_args
add_fitler('acf/load/field/key=field_XXXXXX', 'my_dynamic_select_field', 20);
function my_dynamic_select_field($field) {
$choices = array();
args = array(
'post_status' => 'publish',
'posts_per_page' => 6,
'meta_key' => '_wp_page_template',
'meta_value' => 'page_people3rd.php'
);
$query = new WP_Query($args);
if (count($query->posts)) {
foreach ($query->posts as $post) {
$choices[$post->ID] = $post->post_title;
}
}
$field['choices'] = $choices;
return $field;
}
You could also yous a post object field or a relationship field and then use https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/ or https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ to alter the query done to populate the page.
function my_relationship_query( $args, $field, $post_id ) {
// only show children of the current post being edited
$args['meta_key'] = '_wp_page_template';
$args['meta_value'] = 'page_people3rd.php';
// return
return $args;
}
// filter for every field
add_filter('acf/fields/relationship/query/key=field_XXXXXXX', 'my_relationship_query', 10, 3);
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.