In this tutorial we will learn how to dynamically populate Advanced custom field select field in WordPress. I was trying a generate a dynamic select list so when I will create a update it from anywhere else it will automatically display in Advanced Custom Field select field.
Advanced custom field how to populate select field PHP Code
First we need PHP array with like, you can use you own custom function, I have used categoryList() in this tutorial.
{
"Programming"=>436,
"Finance"=>437
}
Suppose $category = categoryList(); contains data in format like shown above
function catListACF( $field ) {
//Change this to whatever data you are using.
$category = categoryList();
$field['choices'] = array();
foreach( $category as $key => $item ){
$field['choices'][$key] = $item;
}
return $field;
}
add_filter('acf/load_field/name=cateogry', 'catListACF');
Remember field name slug should be changed in add_filter(‘acf/load_field/name=cateogry’, ‘catListACF’);, here used category.
Use this php code using in your functions.php file.
You can also read our tutorial on advance custom filed Advanced custom field how to populate select field.
If you have and query or suggestion then please comment, I will reply to fix your problem, if you like our content then you can subscribe to our Youtube channel. If you want to hire me then reach us at our Fiverr.