How to retreive subfield value

Support MB Group How to retreive subfield value

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #3728
    IvoIvo
    Participant

    Hello 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!

    #3737
    Anh TranAnh Tran
    Keymaster

    Hi @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 🙂

Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘How to retreive subfield value’ is closed to new replies.