Support

Account

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>';
    	}
    }
    ?>