Home Forums General Issues Reverse order, get last row…


Reverse order, get last row…

  • Hi,

    I have a repeater field associated with users that outputs invoice totals (see attached).

    I have it outputting in reverse order, but ideally I would like to only show the most recent/latest row (3, in this case).

    <?php $current_user = wp_get_current_user(); ?>                                              
    $repeater = get_field('invoices', 'user_' . $current_user->ID);
    $order = array();
    foreach( $repeater as $i => $row ) {
    $order[ $i ] = $row['invoice_number'];
    array_multisort( $order, SORT_DESC, $repeater );
    if( $repeater ): ?>
    <?php foreach( $repeater as $i => $row ): ?>
    <li><?php echo $row['invoice_number']; ?>. <?php echo $row['invoice_total']; ?></li>
    <?php endforeach; ?>
    <?php endif; ?>

    Can anyone help?

    // initialize a counter
    $counter = 0;
    foreach ($repeater as $i => $row) {
      if ($counter == 3) {
      // output row
      // increment counter
  • Thanks John,

    Two questions;

    1. Where abouts would this be placed, exactly?
    2. Will this always show the latest/last row? It might not be 3, that was just an example of what should be showing in this instance. There could be 20/30/50 rows.
    3. Thanks

  • You initialize the counter before you’re loop. You put the if statement at the start of your loop. You increment the counter at the end of the loop.

    This will show the first X items set by the if statement. Since you are reversing the order of the repeater this means it will show the last X items in reverse order. You can set X for whatever number you want to show. It will show up to that number, so if there are only 2 items then it would only show 2.

  • Thanks John, much appreciated 👍

  • Edit – Fixed, it was my mistake.

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

You must be logged in to reply to this topic.