Support Forum
Support › MB Settings Page › Getting Field Value
I am having huge problems getting my settings page values,
I have tried both
$value = rwmb_meta( $field_id, array( 'object_type' => 'setting' ), $option_name );
And
$settings = get_option( 'option_name' );
$field_id = 'your_field_id';
if ( isset( $settings[$field_id] ) ) {
return $settings[$field_id];
}
Both don't seem to work, I am just getting an empty return.
I am just adding the code to a subfolder with an included file in the child theme, all the other metabox code that is working fine is in the same file.
What could be the issue?
Ok an update, I did some digging and due to some of the loops I was running I used a few nested groups to manage data like group=>group so I cleaned that up with an array merge, but it still doesn't work there are still nested groups but all have a purpose.
So then I thought I would try getting the rwmb_meta for the first parent group.... BANG it returned all the values that had been changed from their default value in an array with the array in that with the key and values.
Is this normal behaviour for rwmb_meta in a group scenario? you have to target the Group ID and them loop through all the results?
Thanks
Hi David,
Yes, that's the normal behaviour. When you define a group, its sub-fields won't have a separated values in the database. Instead their value is stored inside the top-level group. So, you need to get the single value for the top-level group, and you'll get an array of all sub-fields values. Then you can parse this array to show whatever you want.
Thanks, Anh, keep up the great work.
just to get your thoughts and for others reading this, would just using something like array_walk_recursive be the best course of action to collect all the keys and values into a single object, like:
$group_values = rwmb_meta( 'group_in_question', array( 'object_type' => 'setting' ), 'settings_option_name' );
$found = [];
array_walk_recursive($group_values, function($value, $key) use (&$found) {
$found[$key] = $value;
});
return $found;
Very nice snippet, David. I've added it to the Library on Github :).