Home › Forums › Add-ons › Repeater Field › order acf repeater-field by datepicker in wp-query
How can I sort repeater fields by datepicker within a wp-query and a category.
This is my source:
<ul class="program-list">
<?php query_posts('cat=2'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if( have_rows('program_repeater') ): ?>
<?php while( have_rows('program_repeater') ): the_row();
$dateDayNumber = date_i18n("d", strtotime(get_sub_field('program_repeater_date')));
$dateMonthNumber = date_i18n("m", strtotime(get_sub_field('program_repeater_date')));
$dateDayOrg = date_i18n("l", strtotime(get_sub_field('program_repeater_date')));
$dateDay = substr($dateDayOrg, 0, 2);
$dateTime = get_sub_field('program_repeater_time');
$specialEvent = get_sub_field('program_repeater_special');
$mainTitle = get_the_title();
$subTitle = get_field('sub_text');
$rowSeatsId = get_sub_field('program_repeater_rowseats_id');
?>
<li>
<div class="specialEvent"><?php if( $specialEvent ): ?><?php echo $specialEvent; ?><?php else: ?> <?php endif; ?></div>
<div class="dateDayNumber"><?php echo $dateDayNumber; ?></div>
<div class="dateMonthNumber"><?php echo $dateMonthNumber; ?></div>
<div class="dateDay"><?php echo $dateDay; ?></div>
<div class="v-line-zick-zack"></div>
<div class="dateTime"><?php echo $dateTime; ?></div>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<div class="mainTitle"><?php echo $mainTitle; ?></div>
<div class="subTitle"><?php echo $subTitle; ?></div>
</a>
<a href="<?php the_permalink(); ?><?php echo "?id=" . $rowSeatsId; ?>" title="<?php the_title_attribute(); ?>">
<div class="ticket-link">Tickets kaufen</div>
</a>
<div class="clear"></div>
</li>
<?php endwhile; ?>
<?php endif; ?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>
Thanks in advance
Joe
if you dont try to merge your posts into one list you could try this:
<ul class="program-list">
<?php query_posts('cat=2'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$repeater = get_field('program_repeater');
foreach( $repeater as $key => $row ) {
$thedate = $row['program_repeater_date'];
$column_id[ $key ] = strtotime($thedate);
}
array_multisort( $column_id, SORT_ASC, $repeater );
foreach( $repeater as $row ){
$dateDayNumber = date_i18n("d", strtotime(get_sub_field('program_repeater_date')));
$dateMonthNumber = date_i18n("m", strtotime(get_sub_field('program_repeater_date')));
$dateDayOrg = date_i18n("l", strtotime(get_sub_field('program_repeater_date')));
$dateDay = substr($dateDayOrg, 0, 2);
$dateTime = get_sub_field('program_repeater_time');
$specialEvent = get_sub_field('program_repeater_special');
$mainTitle = get_the_title();
$subTitle = get_field('sub_text');
$rowSeatsId = get_sub_field('program_repeater_rowseats_id');
?>
<li>
<div class="specialEvent"><?php if( $specialEvent ): ?><?php echo $specialEvent; ?><?php else: ?> <?php endif; ?></div>
<div class="dateDayNumber"><?php echo $dateDayNumber; ?></div>
<div class="dateMonthNumber"><?php echo $dateMonthNumber; ?></div>
<div class="dateDay"><?php echo $dateDay; ?></div>
<div class="v-line-zick-zack"></div>
<div class="dateTime"><?php echo $dateTime; ?></div>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<div class="mainTitle"><?php echo $mainTitle; ?></div>
<div class="subTitle"><?php echo $subTitle; ?></div>
</a>
<a href="<?php the_permalink(); ?><?php echo "?id=" . $rowSeatsId; ?>" title="<?php the_title_attribute(); ?>">
<div class="ticket-link">Tickets kaufen</div>
</a>
<div class="clear"></div>
</li>
<?php } ?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>
If you try to merge them => you need to build a array first, order it, and echo it after that.
try to adapt a solution like this or this
Alternatively, You will need to create a custom query using either the get_posts() or wp_Query and have the posts filtered by the date picker values as the meta_keys. But because you do not know the row number, you will have to create a custom function to change the standard ‘=’ to ‘LIKE’ in the query.
Please see: https://support.advancedcustomfields.com/forums/topic/sort-posts-by-date-picker-in-repeater-field/
I hope that helps
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.