Home Forums General Issues Sort custom fields by custom field value


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:

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

    A file to download
    Another file to download

    Indeed this is another file
    You guessed it…

    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">
                <?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; ?>
            <?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.