Home Forums Add-ons Repeater Field Sort by Repeater Date Field Reply To: Sort by Repeater Date Field

  • Thanks @elliot for the reply.

    I’ve started to look at the SQL method as I think this may work, however, I’ve come unstuck with the code. I know if I alter the SQL a little I can get it more or less to work for one month.

    So I’ve altered the code to loop for the next 6 months but it only returns 1 result

    Here’s my code:

    for ($x=1; $x<=6; $x++) {
    	#echo "Month is: $x <br>";
    	$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
    	// loop through the results
    	if( $rows ) {
    		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>
    		echo '</ul>';

    I think I’m being daft as I believe the above “should” work. Any help is much appreciated!