Items in group field are out of order

Support MB Blocks Items in group field are out of orderResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35907
    alpha.mediaalpha.media
    Participant

    When using the group field within the block editor, items do not appear in the order they are output to the screen on the front-end. This appears to happen when you have a cloneable group that you can sort.

    In my case the order in the block editor does not match the array output on the front-end.

    $meta_boxes[] = [
            'title'           => 'Highlighted Content',
            'id'              => 'highlighted-content',
            'description'     => 'Showcases highlighted content on the homepage in the full-width content module.',
            'icon'            => 'pressthis',
            'category'        => 'homepage',
            'keywords'        => ['highlighted'],
            'supports'        => [
                'align' => ['full'],
            ],
            'render_template' => get_template_directory() . '/includes/blocks/highlighted-content/block-template.php',
            'type'            => 'block',
            'context'         => 'side',
            'fields'          => [
                [
                    'name'              => 'Section Title',
                    'id'                => 'title',
                    'type'              => 'text',
                    'label_description' => 'Leave blank if you do not want a title for the section.',
                ],
                [
                    'name'     => 'Background',
                    'id'       => 'background',
                    'type'     => 'select',
                    'default'  => 'is-brand',
                    'options'  => [
                        'is-brand' => 'Brand',
                        'is-light' => 'Light',
                    ],
                ],
                [
                    'name'     => 'Image Size',
                    'id'       => 'image_size',
                    'type'     => 'select',
                    'options'  => [
                        'rectangle' => 'Rectangle',
                        'square' => 'Square',
                    ],
                    'required' => true,
                ],
                [
                    'name'   => 'Posts',
                    'id'     => 'posts',
                    'type'   => 'group',
                    'group_title' => '{highlighted_post}',
                    'clone' => true,
                    'sort_clone' => true,
                    'max_clone' => 8,
                    'fields' => [
                        [
                            'name'              => 'Post',
                            'id'                => 'highlighted_post',
                            'type'              => 'post',
                            'label_description' => 'Select an item to display.',
                            'post_type'         => [
                                'post',
                                'page',
                                'contests',
                                'galleries',
                                'shows',
                                'videos',
                                'tribe_events',
                            ],
                            'field_type'        => 'select_advanced',
                            'query_args'        => [
                                'number' => 5,
                            ],
                            'js_options' => [
                                'minimumInputLength' => 4,
                            ],
                        ],
                    ],
                ],
            ]
        ];
    #35914
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Can you please share your code that output the field value? I've run your code on my local site and use this code to output the field post and the order of the cloneable field display as well on the frontend.

    $groups = mb_get_block_field( 'posts' );
    foreach ( $groups as $group ) {
    	$post_id = $group['highlighted_post'];
    	echo get_the_title( $post_id );
    	echo "<br>";
    }
    #35922
    alpha.mediaalpha.media
    Participant

    I figured out the issue. It was on my end. I was grabbing all the post IDs and using the post__in query argument and realized that doesn't order them by the post ID order.

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