Support

Account

Home Forums Add-ons Repeater Field Filter Repeater row by Date Field

Solved

Filter Repeater row by Date Field

  • We are working on a site for a youth rugby team which has pages for the various age groups, we therefore have to be able to limit which coaches etc can amend each match/fixture. We have added a Matches repeater field to that age groups page and limited the access to that page. Obviously we only want to display upcoming games and not games that have already been played.

    This is the code I have so far:

    <?php if(get_field('matches')): ?>
     
    	<ul class="matches_list">
     
    	<?php while(has_sub_field('matches')): ?>
     
    		<li class="match">Hove (<?php the_sub_field('teams'); ?>) <strong><?php the_sub_field('hove_score'); ?> <span style="color: #A2224C; font-weight: bold;">-</span> <?php the_sub_field('opponent_score'); ?></strong> <?php the_sub_field('opponent'); ?><br><?php the_sub_field('date'); ?> @ <?php the_sub_field('location'); ?></li>
     
    	<?php endwhile; ?>
     
    	</ul>
     
    <?php endif; ?>

    However I need to be able to filter out any of the old games by querying the_sub_field(‘date’), but am not sure how I would go about this. I would also like to restrict it to the next 5 games only.

    Any help would be appreciated!

  • Hi!

    If each post had it’s own date field not in a repeater it’d been easier.. but as it is I suppose you’ll have to do something like this:

    
    
    <?php if(get_field('matches')): 
    $i = 0; 
    $today = date('Y-m-d'); //This has to match the format of your date field
    ?>
    	<ul class="matches_list">
     
    		<?php while(has_sub_field('matches')): ?>
    			<?php if(strtotime($today) <= strtotime(get_sub_field('date'))){ ?>
    				<?php if($i < 5){ ?>
    					<?php $i++; ?>
    					<li class="match">
    						Hove (<?php the_sub_field('teams'); ?>) <strong><?php the_sub_field('hove_score'); ?> <span style="color: #A2224C; font-weight: bold;">-</span> <?php the_sub_field('opponent_score'); ?></strong> <?php the_sub_field('opponent'); ?><br><?php the_sub_field('date'); ?> @ <?php the_sub_field('location'); ?>
    					</li>
    				<?php } ?>
    			<?php } ?>
    		<?php endwhile; ?>
     
    	</ul>
     
    <?php endif; ?>
    
    

    This will not check to make sure the five chosen are the latest ones and it’s possible that due to the repeaterfield arrays setup you will end up with the five oldest ones (which have a date later or equal to today). If so you’ll have to first get the repeaterfield into a variable and do an array_reverse() on it!

  • Thanks for you quick reply,

    This worked perfectly!

  • Now that the season has started I’ve noticed that, as you predicted, the oldest ‘Latest Results’ are being displayed.

    http://hoverugby.co.uk/home-u13/

    Could you provide any further advise on reversing the order based on the date sub field?

    Many thanks!

  • Many thanks to @ashley it gives me new idea of several things. Hopefully the games I am doing maybe soon works on wider platform, as right now they were in flash games sites of http://www.igamesforgirls.com/.

  • Amazing!! Thanks for the solution @jonathan. ACF really does wonders!!

  • Hello,

    I’m coming back on this old post 🙂

    Jonathan’s answer was very helpful for me.

    However, I need my dates to have a format “d m Y”.

    If I change this value in php (coinciding with formt in backoffice of course) => this doesn’t work, nothing displays.
    And if I try format “%d/%m/%Y”, past events are appearing.

    Do I need to convert my date, or something like that ?

    Thanks in advance for your help 🙂

  • Hi,

    I think your issue is that your format is not supported by strtotime.
    Try changing the if to:
    <?php if ( strtotime( $today ) <= strtotime( get_sub_field( 'date', false ) ) ) { ?>
    The second parameter of get_sub_field will return the date unformatted in the way it is saved by ACF (which is Y-m-d H:i:s) and will work with strtotime.

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

The topic ‘Filter Repeater row by Date Field’ is closed to new replies.