Home › Forums › Add-ons › Repeater Field › Sort by Repeater Date Field › Reply To: Sort by Repeater Date Field
Ok, this now works as I need it. I wonder if the code can be tidied in anyway but it works. Here’s my code incase it helps anyone else.
<?php
#start from current month. Change 6 to however months ahead you want
for ($x=0; $x<=6; $x++) {
$date = new DateTime("$x months");
$date->modify("-" . ($date->format('j')-1) . " days");
#echo $date->format('j, m Y');
$month = $date->format('m');
$year = $date->format('Y');
#echo 'Month= '.$month .' Year= '.$year.' <br>'; #debug
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM upKeep_postmeta
WHERE meta_key LIKE %s
AND meta_value LIKE %s
",
'dates_%_available_dates', // meta_name: $ParentName_$RowNumber_$ChildName
#''.$year.''.$month.'%' // meta_value: 20131031 for example
''.$year.''.$month.'%' // meta_value: 20131031 for example
));
// loop through the results
if( $rows ) {
echo '<h2>'.$date->format('F').' '.$date->format('Y').'</h2>';
echo '<ul>';
foreach( $rows as $row ) {
// for each result, find the 'repeater row number' and use it to load the sub field!
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'dates_' . $matches[0] . '_dates'; // $matches[0] contains the row number!
?>
<li><a href="<?php get_permalink( $row->post_id ); ?>"><?php echo get_the_title( $row->post_id ); ?></a></li>
<?php
}
echo '</ul>';
}
}
?>
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 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.