Support Forum
Support › Meta Box Builder › Add Dropdown select of saved data using functions.phpResolved
Hi,
I'm familiar with creating custom meta boxes using php, but for a recent project I've been using the builder, and it's great!
However, I would like to add a dropdown to a clonable field, top be populated from saved information in another custom field ... and I'm struggling. Any pointers?
So I have:
Field group: id= 'maintenance-and-faults'
Inside that a clonal group, id: fault-reporting
I want to allow the user to select an item of faulty equipment which is in another field group which contains a clonable list of equipment.
Field group: 'instument-details'
Clonable group: 'group_j7gfnvh6y4t'
Field: id 'serial_no'
I'm pretty sure I could generate that code in functions.php but how can I hook it into the editor to add it to the bottom of the field group?
I'm trying a different tack using the Select Advanced field and a callback function.
This is my function, but it's not populating the dropdown yet...
function populate_my_select_field() {
global $post;
$options = [];
// Check if we are in the admin area and a post object exists
if (is_admin() && !empty($post)) {
$post_id = $post->ID;
// Fetch the meta values from the specified group for the current post
$groups = rwmb_meta('group_j7gfnvh6y4t', '', $post_id);
foreach ($groups as $group) {
// Assuming 'height' and 'serial_no' are keys in your group
$height = $group['height'] ?? '';
$serial_no = $group['serial_no'] ?? '';
if ($height && $serial_no) {
$key = $serial_no; // Using serial_no as the value
$options[$key] = "Height: $height, Serial No: $serial_no";
}
}
}
return $options;
}
found my way through this, in case anyone else needs it:
function populate_my_select_field() {
//global $post;
$options = [];
$post_id = null;
// Check if we are in the admin area and a post object exists
if (is_admin()) {
$post_id = $_GET['post'] ?? get_the_ID();
error_log('Current post ID: ' . $post_id);
// Fetch the meta values from the specified group for the current post
$groups = rwmb_meta('group_j7gfnvh6y4t', '', $post_id);
error_log(print_r($groups, true));
foreach ($groups as $group) {
// Assuming 'height' and 'serial_no' are keys in your group
$height = $group['height'] ?? '';
$serial_no = $group['serial_no'] ?? '';
if ($height && $serial_no) {
$key = $serial_no; // Using serial_no as the value
$options[$key] = "Height: $height, Serial No: $serial_no";
}
}
}
return $options;
}
While I can see my options in the select dropdown, the chosen item is not saved when the post is updated.
I tested with this simplified version:
function populate_my_select_field() {
return [
'option1' => 'Option 1',
'option2' => 'Option 2',
];
}
But when I populate using fields from the post, the select box is refreshed each time. How do I get it to commit? One way would be to check through the entries each time in the function and compare to the current value of the dropdown... but is there a more efficient way?
function populate_my_select_field() {
return [
'option1' => 'Option 1',
'option2' => 'Option 2',
];
}
And the dynamically populated version:
function populate_my_select_field() {
$options = [];
$post_id = null;
if (is_admin()) {
$post_id = $_GET['post'] ?? get_the_ID();
}
if ($post_id) {
// Define the groups and their corresponding prefixes
$group_ids = [
'group_j7gfnvh6y4t' => 'Anem', // Anemometers
'vanes' => 'Vane', // Wind Vanes
'miscelaneou' => '' // Miscellaneous
];
foreach ($group_ids as $group_id => $prefix) {
$groups = rwmb_meta($group_id, '', $post_id);
// Check if $groups is an array before iterating
if (is_array($groups)) {
foreach ($groups as $group) {
$height = $group['height'] ?? '';
$serial_no = isset($group['serial_no']) ? trim($group['serial_no']) : ''; // Trim the serial number
$type = $group_id === 'miscelaneou' ? ($group['type'] ?? 'Unknown') : '';
if ($height && $serial_no) {
// Construct a unique key and label for the option
//$key = $group_id . '_' . $serial_no;
//$label = ($prefix ?: $type) . " " . $height . "m, Serial No ". $serial_no;
$key = $serial_no;
$label = $serial_no;
$options[$key] = $label;
}
}
}
}
}
return $options;
}
Can anyone help me with getting the dynamically populated values to save?
Hello,
If you use the simplified version
function populate_my_select_field() {
return [
'option1' => 'Option 1',
'option2' => 'Option 2',
];
}
then the field still doesn't save value. Please check if the setting Save field value
is enabled, screenshot https://imgur.com/rdIYYyy
Hi,
I've tested with the simplified version and it works as expected; the values are saved and I'm able to retrieve them on the front end.
Lee