Home › Forums › Add-ons › Repeater Field › Sort repeater field by date sub field
I’ve been trying all sort of things to get this working and I can’t seem to figure it out.
What I have below works great. However, I’ve been trying all different kinds of queries to try and get it to sort by the sub field ‘company_date’. I’ve tried looking at the documentation and examples on this site and just cannot get it to work.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post();
if( have_rows('company_rates') ):
while ( have_rows('company_rates') ) : the_row();
$percentage = get_sub_field('plus_minus');
if ($percentage == "plus") {
$percentageType = "+";
$percentageClass = "circleUp";
} else if ($percentage == "minus") {
$percentageType = "-";
$percentageClass = "circleDown";
}
$date = DateTime::createFromFormat('Ymd', get_sub_field('company_date'));
echo '<article class="item">
<span class="metaDate">'.$date->format('d / M / Y').'</span>';
$posts = get_sub_field('company');
if( $posts ):
foreach( $posts as $post):
setup_postdata($post);
echo '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';
endforeach;
wp_reset_postdata();
endif;
echo '<span class="bigPecent">'.$percentageType . get_sub_field('percentage_number').'%</span>
<span class="'.$percentageClass.'"></span>
</article>';
endwhile;
endif;
?>
<?php endwhile;?>
<?php endif;?>
One of the things I’ve tried was this:
$repeater = get_field('company_rates');
foreach( $repeater as $key => $row ) {
$thedate = $row['company_date'];
$column_id[ $key ] = strtotime($thedate);
}
array_multisort( $column_id, SORT_ASC, $repeater );
foreach( $repeater as $row ){
// rest of my code here
}
However I couldn’t get any of my other sub fields to display.
Has anyone successfully managed this before, what am I missing?
Hi @moofin
Could you please tell me how did you get the other subfields? Could you please share the code?
Keep in mind that with the get_field() function instead of have_rows() function, you need to get the subfields using the field name as the array key like this:
foreach( $repeater as $row ){
echo $row['subfield_name'];
}
Also, please make sure that you’ve set an empty array for your $column_id
variable like this:
$column_id = array();
I hope this helps.
Thanks James, that was really helpful! I think I must have missed out the $column_id and also not calling the $row properly. I thought I’d add in my finished code in case it helps someone:
<?php
$repeater = get_field('company_rates');
$column_id = array();
foreach( $repeater as $key => $row ) {
$thedate = $row['company_date'];
$column_id[ $key ] = strtotime($thedate);
}
array_multisort( $column_id, SORT_DESC, $repeater );
foreach( $repeater as $row ){
$date = DateTime::createFromFormat('Ymd', $row['company_date']);
echo ' <article class="item">
<span class="metaDate">'.$date->format('d / M / Y').'</span>';
// This is pulling in a (company) relationship field I have
$posts = $row['company'];
if( $posts ):
foreach( $posts as $post):
setup_postdata($post);
echo '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';
endforeach;
wp_reset_postdata();
endif;
echo ' <span class="bigPecent">'.$row['percentage_number'].'%</span>
</article>';
}
?>
The topic ‘Sort repeater field by date sub field’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.