Support

Account

Home Forums General Issues Sort custom fields by custom field value

Helping

Sort custom fields by custom field value

  • I’m trying to build an archive of files that are ordered by date (a custom text field).

    I have a repeater (shareholder_meeting) with the following sub_fields:
    shareholder_meeting__file
    shareholder_meeting__title
    shareholder_meeting__year

    I want to display this on my site like this (with a shortcode):

    2015
    A file to download
    Another file to download

    2014
    Indeed this is another file
    You guessed it…

    2013
    This is getting tiresome

    I’m not quite sure of how to do this. I did read documentation on this, but I’m still not quite clear as my PHP skills leave much to be desired. I was hoping I could get some help here.

    This is what my code looks like right now, and without adjustments for sorting by year.

    // List Shareholder meetings
    add_shortcode( 'shareholder-meeting', 'xxx_shareholder_meeting_shortcode' );
    
    function xxx_shareholder_meeting_shortcode( $atts ) {
    
        ob_start(); ?>
    
        <?php if ( have_rows('shareholder_meeting', 425) ): ?>
            <div class="link-list">
                <ul>
                <?php while( have_rows('shareholder_meeting', 425) ): the_row();
                    $shareholder_meeting__file = get_sub_field('shareholder_meeting__file', 425);
                    $shareholder_meeting__title = get_sub_field('shareholder_meeting__title', 425);
                    $shareholder_meeting__year = get_sub_field('shareholder_meeting__year', 425);
                ?>
                    <?php echo $shareholder_meeting__year; ?>
                    <li class="link-list__link"><a href="<?php echo $shareholder_meeting__file; ?>" target="_blank"><?php echo $shareholder_meeting__title; ?></a></li>
                <?php endwhile; ?>
                </ul>
            </div>
            <?php endif; ?>
    
        <?php $termsandforms = ob_get_clean();
        return $termsandforms;
    }
  • Hi Johan,

    I think you’ll have to do a loop of your repeater first. Create a new multidimensional array variable which’ll hold each year as a key and an array of the fields as values. Then loop through that instead.

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

The topic ‘Sort custom fields by custom field value’ is closed to new replies.