Support

Account

Home Forums Add-ons Repeater Field Calendar by month with date repeater Reply To: Calendar by month with date repeater

  • Hi @trainsrenton

    You need to use the $rows variable that holds the wpdb result. Something like this:

    $rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT * 
                FROM {$wpdb->prefix}postmeta
                WHERE meta_key LIKE %s
                ",
                'calendrier_%_jour_debut'
            ));
    
    // vars
    $order = array();
    
    // populate order
    foreach( $rows as $i => $row ) {
    	
    	$order[ $i ] = $row['meta_value'];
    	
    }
    
    // multisort
    array_multisort( $order, SORT_ASC, $rows );
    
    // Temporary month variable
    $current_month = null;
    
    // Loop through the returned rows
    foreach( $rows as $row){
        
        // Convert the date string to PHP datetime
        $date = DateTime::createFromFormat('Ymd', $row['meta_value']);
        
        // Get the month
        $month = $date->format('F');
        
        // Echo the months only once
        if( $current_month != $month ){
            echo $month . "\r\n";
            $current_month = $month;
        }
        
        // Show the date string
        echo $row['meta_value'] . "\r\n";
        
        // Get the related post
        $thepost = get_post($row['post_id']);
        
        // Show it if you want
        print_r($thepost);
        
        // Get the event repeater for the related post
        $calendrier = get_field('calendrier', $thepost->ID);
        
        // Show it if you want
        print_r($calendrier);
    }

    Hope this helps 🙂