Support

Account

Home Forums Add-ons Repeater Field Show repeater rows according to date parameters

Solved

Show repeater rows according to date parameters

  • I have a repeater field in a site options page, which I am using to show content in posts according to category.

    I am attempting to show a div ONLY if the date (real time) falls on or between two dates (start_date and end_date) set in a pair of sub fields within that repeater. The repeater field options (see attached screenshots) specify when it should be shown, filtered by:

    1. Post category (checkbox sub field)
    2. Start date (date picker sub_field)
    3. End date (date picker sub field)

    For this I need to be able to query the rows within the repeater for these variables. Here’s what I have attempted but so far not with any success. The category-specific part of the code works fine, but once the date-specific code is added nothing is returned.

    I’ve based my code on this article and on this thread. I realise the two if conditionals (for category and date) should be combined into a single query but I haven’t been able to do this without the whole page going down.

    <?php if ( have_rows('in_post', 'option') ): ?>
    <?php while ( have_rows('in_post', 'option') ) : the_row(); ?>
    	
    	<?php
    	$category = get_sub_field('category'); //set to 'Reviews'
    	if ($category && in_array('Reviews', $category)): ?> 
    
    		<?php 
    		$today = date('Ymd');
    		$start_date = get_sub_field('start_date'); //set to 09/04/2018
    		$end_date = get_sub_field('end_date'); //set to 15/04/2018
    		if ((strtotime($today) >= strtotime($start_date)) && (strtotime($today) <= strtotime($end_date))): ?>
    			
    			<div class="inline-ad">
    				<a href="<?php the_sub_field('target_link'); ?>" target="_blank"><img src="<?php the_sub_field('full_size'); ?>"></a>
    			</div>
    
    		<?php endif; ?>
    
    	<?php endif; ?>
    	
    <?php endwhile; ?>
    <?php else : endif; ?>

    Page where div should be visible.

  • This has been solved with some help from the ACF team. The issue is the output format on the date picker fields. In order for this to work these must be set to Ymd.

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

You must be logged in to reply to this topic.