Support Forum
Support › MB Relationships › Querying nested group values from a relationshipResolved
Hi,
I'm trying to get my head around this query and not sure which way to approach it.
I have a CPT of Job with relationships to 2 different user roles (um_client and um_sitter).
add_action( 'mb_relationships_init', function () {
MB_Relationships_API::register( [
'id' => 'jobs_to_clients',
'from' => [
'object_type' => 'post',
'post_type' => 'job',
'meta_box' => [
'title' => 'Job for client',
],
],
'to' => array(
'object_type' => 'user',
'field' => array(
'max_clone' => 1,
'query_args' => array(
'role__in' => array('um_client')
)
)
)
] );
} );
add_action( 'mb_relationships_init', function () {
MB_Relationships_API::register( [
'id' => 'jobs_to_sitters',
'from' => [
'object_type' => 'post',
'post_type' => 'job',
'meta_box' => [
'title' => 'Job for sitter',
],
],
'to' => array(
'object_type' => 'user',
'field' => array(
'query_args' => array(
'role__in' => array('um_sitter')
)
)
)
] );
} );
Within the client user data I have some fields which are in nested groups. Here's the portion of the code for these as it's lengthy - The parent group (id - group_pet_details) is at the root level of the form and not cloned but the child group (id - group_pet) is cloneable.
[
'id' => $prefix . 'group_pet_details',
'type' => 'group',
'fields' => [
[
'id' => $prefix . 'pets_number',
'type' => 'number',
'name' => esc_html__( 'Number Of Pets', 'text-domain' ),
'std' => 1,
'min' => 1,
'max' => 5,
'required' => 1,
],
[
'id' => $prefix . 'group_pet',
'type' => 'group',
'name' => esc_html__( 'Specify details for each of your pets.', 'text-domain' ),
'fields' => [
[
'id' => $prefix . 'pet_name',
'type' => 'text',
'name' => esc_html__( 'Pet\'s Name', 'text-domain' ),
'required' => 1,
],
[
'id' => $prefix . 'pet_photo',
'type' => 'image_upload',
'name' => esc_html__( 'Upload a photo of this pet', 'text-domain' ),
'max_status' => 1,
'max_file_uploads' => 1,
],
[
'id' => $prefix . 'pet_dob_month',
'name' => esc_html__( 'Which month was your pet born in?', 'text-domain' ),
'type' => 'select_advanced',
'options' => [
'January' => esc_html__( 'January', 'text-domain' ),
'February' => esc_html__( 'February', 'text-domain' ),
'March' => esc_html__( 'March', 'text-domain' ),
'April' => esc_html__( 'April', 'text-domain' ),
'May' => esc_html__( 'May', 'text-domain' ),
'June' => esc_html__( 'June', 'text-domain' ),
'July' => esc_html__( 'July', 'text-domain' ),
'August' => esc_html__( 'August', 'text-domain' ),
'September' => esc_html__( 'September', 'text-domain' ),
'October' => esc_html__( 'October', 'text-domain' ),
'November' => esc_html__( 'November', 'text-domain' ),
'December' => esc_html__( 'December', 'text-domain' ),
'Not known' => esc_html__( 'Not known', 'text-domain' ),
],
'required' => 1,
],
[
'id' => $prefix . 'pet_dob_year',
'name' => esc_html__( 'Which year was your pet born in?', 'text-domain' ),
'type' => 'select_advanced',
'options' => [
2020 => esc_html__( '2020', 'text-domain' ),
2019 => esc_html__( '2019', 'text-domain' ),
2018 => esc_html__( '2018', 'text-domain' ),
2017 => esc_html__( '2017', 'text-domain' ),
2016 => esc_html__( '2016', 'text-domain' ),
2015 => esc_html__( '2015', 'text-domain' ),
2014 => esc_html__( '2014', 'text-domain' ),
2013 => esc_html__( '2013', 'text-domain' ),
2012 => esc_html__( '2012', 'text-domain' ),
2011 => esc_html__( '2011', 'text-domain' ),
2010 => esc_html__( '2010', 'text-domain' ),
2009 => esc_html__( '2009', 'text-domain' ),
2008 => esc_html__( '2008', 'text-domain' ),
2007 => esc_html__( '2007', 'text-domain' ),
2006 => esc_html__( '2006', 'text-domain' ),
2005 => esc_html__( '2005', 'text-domain' ),
2004 => esc_html__( '2004', 'text-domain' ),
2003 => esc_html__( '2003', 'text-domain' ),
2002 => esc_html__( '2002', 'text-domain' ),
2001 => esc_html__( '2001', 'text-domain' ),
2000 => esc_html__( '2000', 'text-domain' ),
1999 => esc_html__( '1999', 'text-domain' ),
1998 => esc_html__( '1998', 'text-domain' ),
1997 => esc_html__( '1997', 'text-domain' ),
1996 => esc_html__( '1996', 'text-domain' ),
1995 => esc_html__( '1995', 'text-domain' ),
1994 => esc_html__( '1994', 'text-domain' ),
1993 => esc_html__( '1993', 'text-domain' ),
1992 => esc_html__( '1992', 'text-domain' ),
1991 => esc_html__( '1991', 'text-domain' ),
1990 => esc_html__( '1990', 'text-domain' ),
'Before 1990' => esc_html__( 'Before 1990', 'text-domain' ),
],
'required' => 1,
],
[
'id' => $prefix . 'pet_sex',
'name' => esc_html__( 'Sex Of Pet', 'text-domain' ),
'type' => 'radio',
'options' => [
'Male' => esc_html__( 'Male', 'text-domain' ),
'Female' => esc_html__( 'Female', 'text-domain' ),
],
'required' => 1,
],
[
'id' => $prefix . 'pet_species',
'name' => esc_html__( 'Type Of Pet', 'text-domain' ),
'type' => 'radio',
'options' => [
'Cat' => esc_html__( 'Cat', 'text-domain' ),
'Dog' => esc_html__( 'Dog', 'text-domain' ),
],
'required' => 1,
],
[
'id' => $prefix . 'pet_breed',
'type' => 'text',
From the Job I need to be able to retrieve all the instances of the field pet_species (and will have others to retrieve as well later).
I already have the ID for the client user and I'm working from within the instance of the Job post but I'm not sure how to access the fields in the nested groups from there.
If you have an example of this it would be awesome.
Thanks.
Hi Martin,
You can just follow the documentation to get the sub-fields value https://docs.metabox.io/extensions/meta-box-group/#getting-sub-field-values.
For example, the returned value when using the helper function to get group_pet_details
is an array with this structure.
array(
'pets_number' => 'value',
'group_pet' => array(
0 => array(
'pet_name' => 'value',
'pet_photo' => 'value'
),
1 => array(
'pet_name' => 'value',
'pet_photo' => 'value'
...
),
...
)
)
Hope that makes sense.
Hi Long,
The use of the functions is quite clear but I'm not getting returned data.
If I use:
$group_pet_details = rwmb_meta('group_pet_details');
$value = isset( $group_pet_detail[pets_number] ) ? $group_pet_detail[pets_number] : 'value not returned';
echo $value; // Display sub-field value
I get the 'value not returned' as the echo response.
The group_pet_details is part of the user meta which is set by relationship rather than saved against the post meta which makes me think I should have some additional level of reference in the field ID.
Hi Martin,
To get the user meta, please follow this documentation https://docs.metabox.io/extensions/mb-user-meta/#getting-field-value.
And to get the value of a key, you have to wrap the key in the single quote $group_pet_detail['pets_number']
.
Ah yes. Thanks. I've got it sorted now. I was expecting something slightly different to what was coming back as well but I've got there now. Somewhat simpler than I was assuming 😉