get post metabox data inside of MB Block Default Value

Support MB Blocks get post metabox data inside of MB Block Default ValueResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35203
    Kyle MeltonKyle Melton
    Participant

    Is it possible to get data from a post level metabox id rating_1 and show it as a default value inside of a MB Block?

    I tried to set variables as below and neither seems to work:

    $rating1 = rwmb_meta( 'rating_1' );
    $rating2 = get_option( 'bxyrating_titles' )['rating_2'];

    Here is the code below:

    add_filter( 'rwmb_meta_boxes', 'bestxy_box' );
    
    function bestxy_box( $meta_boxes ) {
    	$rating1 = rwmb_meta( 'rating_1' );
    	$rating2 = get_option( 'bxyrating_titles' )['rating_2'];
    	$rating3 = get_option( 'bxyrating_titles' )['rating_3'];
    	$rating4 = get_option( 'bxyrating_titles' )['rating_4'];
    	$rating5 = get_option( 'bxyrating_titles' )['rating_5'];
    	$rating6 = get_option( 'bxyrating_titles' )['rating_6'];
        $prefix = '';
    
        $meta_boxes[] = [
            'title'       => __( 'Best XY Product Box', 'bestxy-box' ),
            'id'          => 'review-highlights-2',
            'icon'        => [
                'background' => '#dd3333',
                'foreground' => '',
                'src'        => 'welcome-widgets-menus',
            ],
            'category'    => 'bbblock',
            'supports'    => [
                'align' => [''],
            ],
            'render_template' => ABSPATH . '/wp-content/plugins/rebuild/tpl/tpl-bestxy-box.php',
            'type'        => 'block',
            'context'     => 'side',
            'fields'      => [
    			[
                    'name'      => __( 'Amazon Product?', 'bestxy-box' ),
                    'id'        => $prefix . 'amz_switch',
                    'type'      => 'switch',
                    'style'     => 'rounded',
                    'on_label'  => 'Yes',
                    'off_label' => 'No',
    				'std'  => true,
                ],
    			[
                    'name'      => __( 'Show Ads?', 'bestxy-box' ),
                    'id'        => $prefix . 'ads_switch',
                    'type'      => 'switch',
                    'style'     => 'rounded',
                    'on_label'  => 'Yes',
                    'off_label' => 'No',
    				'std'  => false,
                ],
                [
                    'name' => __( 'ASIN', 'bestxy-box' ),
                    'id'   => $prefix . 'asin',
                    'type' => 'text',
    				'visible'           => [
                        'when'     => [['amz_switch', '=', 1 ]],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name' => __( 'Product Title', 'bestxy-box' ),
                    'id'   => $prefix . 'product_title',
                    'type' => 'text',
                ],
                [
                    'id'   => $prefix . 'highlight_primary_color',
                    'type' => 'hidden',
                    'std'  => '#0e6838',
                ],
                [
                    'id'   => $prefix . 'secondary_color',
                    'type' => 'hidden',
                    'std'  => '#ae362b',
                ],
                [
                    'name'    => __( 'Award Image?', 'bestxy-box' ),
                    'id'      => $prefix . 'award_y_n',
                    'type'    => 'radio',
                    'options' => [
                        'Yes' => __( 'Yes', 'bestxy-box' ),
                        'No'  => __( 'No', 'bestxy-box' ),
                    ],
                    'std'     => 'No',
                    'columns' => 6,
                    'inline'  => false,
                ],
                [
                    'name'              => __( 'Award Img', 'bestxy-box' ),
                    'id'                => $prefix . 'award_img',
                    'type'              => 'image_select',
                    'label_description' => __( 'Field for award image options (coming soon)', 'bestxy-box' ),
                    'options'           => [
                        'Top Pick'         => __( 'https://www.birdinformer.com/wp-content/uploads/2021/10/Top-Pick-1-min.png', 'bestxy-box' ),
                        'Editor\'s Choice' => __( 'https://www.birdinformer.com/wp-content/uploads/2021/10/Editors-Choice-min.png', 'bestxy-box' ),
                        'Best Value'       => __( 'https://www.birdinformer.com/wp-content/uploads/2021/10/Best-Value-min.png', 'bestxy-box' ),
                    ],
                    'columns'           => 6,
                    'visible'           => [
                        'when'     => [['award_y_n', '=', 'Yes']],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name' => __( 'Best of', 'bestxy-box' ),
                    'id'   => $prefix . 'award',
                    'type' => 'text',
                ],
    			[
                    'name'       => __( 'Image', 'bestxy-box' ),
                    'id'         => $prefix . 'non_amz_image',
                    'type'       => 'single_image',
                    'image_size' => 'medium_large',
    				'visible'           => [
                        'when'     => [['amz_switch', '=', 0 ]],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name' => __( 'Amazon Image', 'bestxy-box' ),
                    'id'   => $prefix . 'amz_img',
                    'type' => 'number',
                    'std'  => 1,
    				'visible'           => [
                        'when'     => [['amz_switch', '=', 1 ]],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name' => __( 'Amazon Image Alt Tag', 'bestxy-box' ),
                    'id'   => $prefix . 'img_alt',
                    'type' => 'text',
    				'visible'           => [
                        'when'     => [['amz_switch', '=', 1 ]],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name' => __( 'Editor\'s Comments', 'bestxy-box' ),
                    'id'   => $prefix . 'comment',
                    'type' => 'wysiwyg',
                ],
                [
                    'name' => __( 'Features List', 'bestxy-box' ),
                    'id'   => $prefix . 'features',
                    'type' => 'wysiwyg',
                ],
                [
                    'name'        => __( 'Ratings', 'bestxy-box' ),
                    'id'          => $prefix . 'progress',
                    'type'        => 'group',
                    'collapsible' => true,
                    'group_title' => 'Progress Bar',
                    'clone'       => true,
                    'sort_clone'  => true,
                    'fields'      => [
                        [
                            'name' => __( 'Rating Title', 'bestxy-box' ),
                            'id'   => $prefix . 'rating_title',
                            'type' => 'text',
                        ],
                        [
                            'name'        => __( 'Rating Bar', 'bestxy-box' ),
                            'id'          => $prefix . 'rating_bar',
                            'type'        => 'text',
                            'placeholder' => __( 'A score from 0 to 100', 'bestxy-box' ),
                        ],
                    ],
    				'std' => [
    					[
    						'rating_title' => $rating1,
    					],
    					[
    						'rating_title' => $rating2,
    					],
    					[
    						'rating_title' => $rating3,
    					],
    					[
    						'rating_title' => $rating4,
    					],
    					[
    						'rating_title' => $rating5,
    					],
    					[
    						'rating_title' => $rating6,
    					],
    				],
                ],
                [
                    'name'     => __( 'Overall Score', 'bestxy-box' ),
                    'id'       => $prefix . 'overall_score',
                    'type'     => 'text',
                    'required' => true,
                ],
                [
                    'name' => __( 'Bottom Line', 'bestxy-box' ),
                    'id'   => $prefix . 'bottom_line',
                    'type' => 'textarea',
                ],
                [
                    'name' => __( 'Pros', 'bestxy-box' ),
                    'id'   => $prefix . 'pro',
                    'type' => 'wysiwyg',
                ],
                [
                    'name' => __( 'Cons', 'bestxy-box' ),
                    'id'   => $prefix . 'con',
                    'type' => 'wysiwyg',
                ],
                [
                    'name' => __( 'Summary', 'bestxy-box' ),
                    'id'   => $prefix . 'summary',
                    'type' => 'wysiwyg',
                ],
                [
                    'name' => __( 'Review Button Text', 'bestxy-box' ),
                    'id'   => $prefix . 'button_txt',
                    'type' => 'text',
                ],
    			[
                    'name' => __( 'Review Button URL', 'bestxy-box' ),
                    'id'   => $prefix . 'button_url',
                    'type' => 'text',
    				'visible'           => [
                        'when'     => [['amz_switch', '=', 0 ]],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name' => __( 'buy Button Text', 'bestxy-box' ),
                    'id'   => $prefix . 'buy_button_txt',
                    'type' => 'text',
                ],
    			[
                    'name' => __( 'Review Button URL', 'bestxy-box' ),
                    'id'   => $prefix . 'buy_button_url',
                    'type' => 'text',
    				'visible'           => [
                        'when'     => [['amz_switch', '=', 0 ]],
                        'relation' => 'or',
                    ],
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    #35230
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please use the function get_post_meta() to get the field value when registering the meta box. Refer to this topic https://support.metabox.io/topic/bug-settings-not-available-for-custom-fields/#post-29610

    For example:

    $post_id = null;
    if ( isset( $_GET['post'] ) ) {
        $post_id = intval( $_GET['post'] );
    } elseif ( isset( $_POST['post_ID'] ) ) {
        $post_id = intval( $_POST['post_ID'] );
    }
    $rating1 = get_post_meta( $post_id, 'rating_1', true );
    
    #35301
    Kyle MeltonKyle Melton
    Participant

    Thank you Long!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.