Home Forums Add-ons Flexible Content Field Random Flexible Content Field Results Ordering


Random Flexible Content Field Results Ordering

  • Hi,

    I’m trying to ‘shuffle’ the results in the flexible content field so that they appear in a random order. I know this is possible with the repeater using a foreach loop (, however I’ve not been able to get the flexible content field to display results with a foreach correctly (I’m not sure where I’m going wrong!).

    I’m getting the results for the current flexible content field using the below:

    <?php if(get_field("slideshow")): ?>
            <?php while(has_sub_field("slideshow")): ?>
                <?php if(get_row_layout() == "image"): ?>
                    <!-- Image Layout Script -->
                <?php elseif(get_row_layout() == "youtube"): ?>
                    <!-- YouTube Layout Script -->
                <?php elseif(get_row_layout() == "vimeo"): ?>
                    <!-- Vimeo Layout Script -->
                <?php endif; ?>
            <?php endwhile; ?>
    <?php endif; ?>

    Is there any options for ordering the FCF using the get_field method? Or is there a way of using a foreach more effectively for a flexible content field?

    Thanks in advance!!

  • Hi @Steve

    You will need to use a foreach loop to iterate over the flexible content layouts.
    The code will look much the same as the repeater fiedl example, exept you will need to look at the ‘acf_fc_layout’ value instead of get_row_layout().

    When you loop through the rows, just print_r the $row to see what data is a available to you


  • Thanks Elliot, that’s a great help. I’ll have a look at this and get back to you with my results!


  • I used this code for sort my flexible content. Basic is the same code for repeater.

        $acontecimentos = get_field('acontecimentos');
        $date = [];
        foreach($acontecimentos as $key => $row) {
            $date[$key] = $row['data'];
        array_multisort($date, SORT_ASC, $acontecimentos);
        foreach($acontecimentos as $row) {
            if($row['acf_fc_layout'] == 'galeria'):

    After array_multisort you can use shuffle

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Random Flexible Content Field Results Ordering’ is closed to new replies.