How to retreive subfield value
- This topic has 1 reply, 2 voices, and was last updated 8 years, 9 months ago by
Anh Tran.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
July 22, 2016 at 10:02 PM #3728
Ivo
ParticipantHello Rilwis, thank you for this great extension, I am able to retrieve the parent values, but not the values from the subfields, here is my code:
add_filter( 'rwmb_meta_boxes', 'saq_metas' ); function saq_metas( $meta_boxes ) { $meta_boxes[] = array( 'title' => __( 'Questions and Answers' ), 'post_types' => 'saq', 'fields' => array( array( 'id' => 'saq_id', 'type' => 'group', 'clone' => true, 'sort_clone' => true, // Sub-fields 'fields' => array( array( 'name' => __( 'Question', 'bs_saq' ), 'id' => 'question', 'type' => 'text', ), array( 'name' => __( 'Answer', 'bs_saq' ), 'id' => "answer", 'type' => 'wysiwyg', 'raw' => false, 'std' => esc_html__( '', 'bs_saq' ), 'options' => array( 'textarea_rows' => 4, 'teeny' => true, 'media_buttons' => false, ), ), array( 'name' => __( 'Supplementary Information', 'textdomain' ), 'id' => 'supplementary', 'type' => 'group', 'clone' => true, 'fields' => array( // Sub title array( 'name' => __( 'Supplementary info title', 'textdomain' ), 'id' => 'sub_text', 'type' => 'text', ), // Sub info array( 'name' => __( 'Supplementary info text', 'textdomain' ), 'id' => 'sub_textarea', 'type' => 'textarea', ), ), ), ), ), ), ); return $meta_boxes; }
I would really appreciate if you can guide me, I tried this:
$group_values = rwmb_meta( 'supplementary' ); foreach ( $group_values as $group_value ) { $value = isset( $group_value['sub_text'] ) ? $group_value['sub_text'] : ''; echo $value; }
but it says to me "Warning: Invalid argument supplied for foreach() in" on line 19 and line 19 is this one:
foreach ( $group_values as $group_value ) {I am confused, please help, thank you!
July 23, 2016 at 8:57 AM #3737Anh Tran
KeymasterHi @Ivo,
The 'Supplementary Information' is a child field of the parent group, so you have to access to its value through the parent value, like this:
$group = rwmb_meta( 'saq_id' ); $supplementary = isset( $group['supplementary'] ) ? $group['supplementary'] : array(); foreach ( $supplementary as $single_supplementary ) { $value = isset( $single_supplementary['sub_text'] ) ? $single_supplementary['sub_text'] : ''; echo $value; }
Just think about group and sub-group as a hierarchy, everything will be clear 🙂
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- The topic ‘How to retreive subfield value’ is closed to new replies.