Home › Forums › General Issues › a new select field with data from query_args
I am slowly moving from Metabox to ACF and wondering how I can do this with ACF.
I have a select field created with Metabox as below
array(
'name' => esc_html__( ‘Author’, 'your-prefix' ),
'id' => "{$prefix}author",
'type' => 'post',
'post_type' => 'page',
'field_type' => 'select_advanced',
'placeholder' => esc_html__( 'Select an Item', 'your-prefix' ),
'query_args' => array(
'post_status' => 'publish',
'posts_per_page' => 6,
'meta_key' => '_wp_page_template',
'meta_value' => 'page_people3rd.php',
),
),
Basically it’s a select box with data collected from pages using template page_people3rd.php. How can I create a field like that in ACF?
You can use a select field and then add a filter to dynamically generate the field’s choices https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Thanks for answering. I actually read that example, but still have no clue how to get data from query_args, like I am using with Metabox right now. Can you help to give me more details how to implement it, please?
Thanks.
OK, I need to combine:
– that https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/ to get the current value of current Metabox.
– and this https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/ for listing all other pages following the $args, in case there is no meta value in the current post.
Now the issue is solved.
Just not sure if there is a better way to achieve?
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);
The topic ‘a new select field with data from query_args’ is closed to new replies.
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.