Support

Account

Home Forums Front-end Issues Sort events by date

Solving

Sort events by date

  • Hi,

    I’ve been using ACF for a long time now, awesome, but I’m stuck.

    I added dates to some posts, but now I want to sort them using the date. I can manage that, but I need something else…
    I want to display a div displaying the date every time a new date is occurring in the posts.

    Something like this:

    
    <?php 
    if (have_posts()) : while(have_posts()) : the_post();
      $acf_date = the_field('event_date');
      if ($acf_date == 'FIRST TIME BEING DISPLAYED'){ ?>
    <div><?php echo $acf_date; ?><?div>
    <?php } endwhile; endif; ?>
    

    To clear things up, I added an image of my idea. Each time there is a new date, a blue square with the date will be displayed.

    I hope this is clear, otherwise I will try to explain it a bit further.

    Thanks in advance,
    Alex

  • Hi @alexnl88

    Hmm… May be you could have an array that records the date field’s values then use in_array() PHP function to check if the array contains some the date value if not, then add it. The code would look something like this:

    <?php 
    $dates = array();
    if (have_posts()) : while(have_posts()) : the_post();
      $acf_date = the_field('event_date');
      if ( !in_array($acf_date, $dates) ){ ?>
    <div><?php echo $acf_date; array_push($dates, $acf_date); ?><?div>
    <?php } endwhile; endif; ?>

    For more information, check out the following links:
    http://www.w3schools.com/php/func_array_push.asp
    http://php.net/manual/en/function.in-array.php

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

The topic ‘Sort events by date’ is closed to new replies.