Populating Advanced Custom Field Select with WordPress Menu choices

If you’ve ever wanted an Advanced Custom Fields Select field with WordPress Menu choices but wanted it to be dynamic, it’s pretty easy to do using the ‘load_field’ filter. That filter allows you to inject new select options into the field dynamically.

Using this filter, we can loop over our WordPress Menu items and then populate the field. Below is the code snippet that you can paste into your own functions.php file – just be sure to call your select field “menu_choice”. That field will now return a menu slug that can be used to fetch the menu.


add_filter('acf/load_field/name=menu_choice', function ($field) {
$menus = get_registered_nav_menus();
foreach ($menus as $key => $value) {
$field['choices'][$key] = $value;
}
return $field;
});

view raw

functions.php

hosted with ❤ by GitHub