Custom Fields
Re-Envisioned
Support MB Blocks Conflict using multiple taxonomy advanced fields in same block

  • Creator
    Topic
  • #31742
    alpha.media
    Participant

    I am running into in issue with having two taxonomy advanced fields that populate using WordPress categories for a featured content section I am building.

    On the very first save, everything is fine. I select my category in one select field and another category in the other. However, when I go back to edit the block, they both somehow get set to the same category. I am able to then re-select and save and it works again until I go back to edit the page.

    It appears there is an issue with using multiple taxonomy select boxes that pull from wordpress categories. Somehow on page load they are setting to the same values and I have to re-choose them again and hit save.

    Just to clarify the two fields I am referencing are "fallback_category" and "autopopulated_single_category"

    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'           => __( 'Featured Content', 'your-text-domain' ),
            'id'              => 'featured-content',
            'description'     => 'Displays featured content on the homepage.',
            'icon'            => 'star-filled',
            'category'        => 'theme',
            'keywords'        => ['featured'],
            'supports'        => [
                'align' => ['full'],
            ],
            'render_template' => '/Users/****/Documents/projects/blacklab/wp-content/themes/blacklab/includes/functions/blocks/featured-content.php',
            'type'            => 'block',
            'context'         => 'side',
            'fields'          => [
                [
                    'name'     => __( 'Content Population', 'your-text-domain' ),
                    'id'       => $prefix . 'content_population',
                    'type'     => 'select',
                    'options'  => [
                        'auto'   => __( 'Auto-populate by category', 'your-text-domain' ),
                        'manual' => __( 'Manually populate', 'your-text-domain' ),
                    ],
                    'std'      => 'autopopulate',
                    'required' => true,
                ],
                [
                    'name'              => __( 'Fallback Category', 'your-text-domain' ),
                    'id'                => $prefix . 'fallback_category',
                    'type'              => 'taxonomy_advanced',
                    'label_description' => __( 'Select a fallback category for when there are not enough posts.', 'your-text-domain' ),
                    'taxonomy'          => ['category'],
                    'field_type'        => 'select_advanced',
                    'required'          => true,
                ],
                [
                    'name'    => __( 'Category Source', 'your-text-domain' ),
                    'id'      => $prefix . 'category_source',
                    'type'    => 'select',
                    'options' => [
                        'singlelocal'   => __( 'Single Local Category', 'your-text-domain' ),
                        'multiplelocal' => __( 'Multiple Local Categories', 'your-text-domain' ),
                        'syndicated'    => __( 'Syndicated Category', 'your-text-domain' ),
                    ],
                    'std'     => 'singlelocal',
                    'visible' => [
                        'when'     => [['content_population', '=', 'auto']],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name'       => __( 'Autopopulated Single Category', 'your-text-domain' ),
                    'id'         => $prefix . 'autopopulated_single_category',
                    'type'       => 'taxonomy_advanced',
                    'taxonomy'   => ['category'],
                    'field_type' => 'select_advanced',
                    'visible'    => [
                        'when'     => [['category_source', '=', 'singlelocal']],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name'       => __( 'Autopopulated Categories', 'your-text-domain' ),
                    'id'         => $prefix . 'autopopulated_categories',
                    'type'       => 'taxonomy_advanced',
                    'taxonomy'   => ['category'],
                    'field_type' => 'select_advanced',
                    'multiple'   => true,
                    'visible'    => [
                        'when'     => [['category_source', '=', 'multiplelocal']],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name'    => __( 'Autopopulated Syndicated Category', 'your-text-domain' ),
                    'id'      => $prefix . 'autopopulated_syndicated_category',
                    'type'    => 'select',
                    'options' => get_syndicated_hub_cat_list(),
                    'visible' => [
                        'when'     => [['category_source', '=', 'syndicated']],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name'          => __( 'Featured Items', 'your-text-domain' ),
                    'id'            => $prefix . 'featured_items',
                    'type'          => 'group',
                    'collapsible'   => true,
                    'default_state' => 'collapsed',
                    'group_title'   => 'Featured Item {#}',
                    'clone'         => true,
                    'sort_clone'    => true,
                    'max_clone'     => 10,
                    'fields'        => [
                        [
                            'name'    => __( 'Featured Item Source', 'your-text-domain' ),
                            'id'      => $prefix . 'featured_item_source',
                            'type'    => 'select',
                            'options' => [
                                'local'      => __( 'Local Post', 'your-text-domain' ),
                                'syndicated' => __( 'Syndicated Post', 'your-text-domain' ),
                                'category'   => __( 'Category', 'your-text-domain' ),
                            ],
                            'std'     => 'local',
                        ],
                        [
                            'name'       => __( 'Featured Item', 'your-text-domain' ),
                            'id'         => $prefix . 'featured_item',
                            'type'       => 'post',
                            'post_type'  => ['post', 'page', 'contests', 'galleries', 'shows', 'videos'],
                            'field_type' => 'select_advanced',
                            'visible'    => [
                                'when'     => [['featured_item_source', '=', 'local']],
                                'relation' => 'or',
                            ],
                        ],
                        [
                            'name'              => __( 'Alternate Image', 'your-text-domain' ),
                            'id'                => $prefix . 'alternate_image',
                            'type'              => 'image_advanced',
                            'label_description' => __( 'Select a different image to appear instead of the featured image.', 'your-text-domain' ),
                            'max_status'        => false,
                        ],
                        [
                            'name'    => __( 'Syndicated Post ID', 'your-text-domain' ),
                            'id'      => $prefix . 'syndicated_post_id',
                            'type'    => 'text',
                            'visible' => [
                                'when'     => [['featured_item_source', '=', 'syndicated']],
                                'relation' => 'or',
                            ],
                        ],
                        [
                            'name'       => __( 'Featured Item Category', 'your-text-domain' ),
                            'id'         => $prefix . 'featured_item_category',
                            'type'       => 'taxonomy',
                            'taxonomy'   => ['category'],
                            'field_type' => 'select_advanced',
                            'visible'    => [
                                'when'     => [['featured_item_source', '=', 'category']],
                                'relation' => 'or',
                            ],
                        ],
                        [
                            'name' => __( 'Enable Item Schedule', 'your-text-domain' ),
                            'id'   => $prefix . 'enable_item_schedule',
                            'type' => 'checkbox',
                        ],
                        [
                            'name'    => __( 'Featured Item Start', 'your-text-domain' ),
                            'id'      => $prefix . 'featured_item_start',
                            'type'    => 'datetime',
                            'visible' => [
                                'when'     => [['enable_item_schedule', '=', true]],
                                'relation' => 'or',
                            ],
                        ],
                        [
                            'name'    => __( 'Featured Item End', 'your-text-domain' ),
                            'id'      => $prefix . 'featured_item_end',
                            'type'    => 'datetime',
                            'visible' => [
                                'when'     => [['enable_item_schedule', '=', true]],
                                'relation' => 'or',
                            ],
                        ],
                    ],
                    'visible'       => [
                        'when'     => [['content_population', '=', 'manual']],
                        'relation' => 'or',
                    ],
                ],
                [
                    'type' => 'heading',
                    'name' => __( 'Static Items', 'your-text-domain' ),
                ],
                [
                    'name'       => __( 'Static Post 1', 'your-text-domain' ),
                    'id'         => $prefix . 'static_post_1',
                    'type'       => 'post',
                    'post_type'  => [
                        'post',
                        'page',
                        'contests',
                        'galleries',
                        'shows',
                        'tribe_events',
                        'videos',
                    ],
                    'field_type' => 'select_advanced',
                ],
                [
                    'name'       => __( 'Static Post 2', 'your-text-domain' ),
                    'id'         => $prefix . 'static_post_2',
                    'type'       => 'post',
                    'post_type'  => [
                        'post',
                        'page',
                        'contests',
                        'galleries',
                        'shows',
                        'tribe_events',
                        'videos',
                    ],
                    'field_type' => 'select_advanced',
                ],
            ],
        ];
    
        return $meta_boxes;
    }
Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #31753
    Long Nguyen
    Moderator

    Hi,

    I do not see any issue with your code on my end. Screen record https://share.getcloudapp.com/7KuAE2Yy

    The code which I tested on the local site

    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'           => __( 'Featured Content', 'your-text-domain' ),
            'id'              => 'featured-content',
            'description'     => 'Displays featured content on the homepage.',
            'icon'            => 'star-filled',
            'category'        => 'theme',
            'keywords'        => ['featured'],
            'supports'        => [
                'align' => ['full'],
            ],
            'render_callback' => 'my_callback',
            'type'            => 'block',
            'context'         => 'side',
            'fields'          => [
                [
                    'name'     => __( 'Content Population', 'your-text-domain' ),
                    'id'       => $prefix . 'content_population',
                    'type'     => 'select',
                    'options'  => [
                        'auto'   => __( 'Auto-populate by category', 'your-text-domain' ),
                        'manual' => __( 'Manually populate', 'your-text-domain' ),
                    ],
                    'std'      => 'autopopulate',
                    'required' => true,
                ],
                [
                    'name'              => __( 'Fallback Category', 'your-text-domain' ),
                    'id'                => $prefix . 'fallback_category',
                    'type'              => 'taxonomy_advanced',
                    'label_description' => __( 'Select a fallback category for when there are not enough posts.', 'your-text-domain' ),
                    'taxonomy'          => ['category'],
                    'field_type'        => 'select_advanced',
                    'required'          => true,
                ],
                [
                    'name'    => __( 'Category Source', 'your-text-domain' ),
                    'id'      => $prefix . 'category_source',
                    'type'    => 'select',
                    'options' => [
                        'singlelocal'   => __( 'Single Local Category', 'your-text-domain' ),
                        'multiplelocal' => __( 'Multiple Local Categories', 'your-text-domain' ),
                        'syndicated'    => __( 'Syndicated Category', 'your-text-domain' ),
                    ],
                    'std'     => 'singlelocal',
                    'visible' => [
                        'when'     => [['content_population', '=', 'auto']],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name'       => __( 'Autopopulated Single Category', 'your-text-domain' ),
                    'id'         => $prefix . 'autopopulated_single_category',
                    'type'       => 'taxonomy_advanced',
                    'taxonomy'   => ['category'],
                    'field_type' => 'select_advanced',
                    'visible'    => [
                        'when'     => [['category_source', '=', 'singlelocal']],
                        'relation' => 'or',
                    ],
                ],
                [
                    'name'       => __( 'Autopopulated Categories', 'your-text-domain' ),
                    'id'         => $prefix . 'autopopulated_categories',
                    'type'       => 'taxonomy_advanced',
                    'taxonomy'   => ['category'],
                    'field_type' => 'select_advanced',
                    'multiple'   => true,
                    'visible'    => [
                        'when'     => [['category_source', '=', 'multiplelocal']],
                        'relation' => 'or',
                    ],
                ],           
            ],
        ];
        return $meta_boxes;
    }
    
    function my_callback( $attributes, $is_preview = false, $post_id = null ) {
        // Fields data.
        if ( empty( $attributes['data'] ) ) {
            return;
        }
        mb_the_block_field( 'fallback_category' );
        echo '<br>';
        mb_the_block_field( 'autopopulated_single_category' );
    
    }
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.