Order results of a query by values

I’m trying to create a function to populate an ajax calendar with events.
I’m querying a date (format Ymd) from custom field in my wordpress database.

        $metakey2 = 'jour';
        $jours = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey2) );
        if ($jours) {
            $years = substr($jours, 0, 4); // OUTPUT YEAR FROM DATE
            $months = substr($jours, 5, 2);  // OUTPUT MONTH FROM DATE
            $days = substr($jours, 7, 2); // OUTPUT DAY FROM DATE

                foreach $years as $year {
                    foreach $months as $month {

                        if ($y == $year AND $m == $month) {
                            $events = array(
                                foreach $days as $day {
                                    $day => array(
                                        0 => array(
                                            "0" => "Lorem ipsum dolor 111", 
                                        )
                                    );
                                }
                            );
                        }
                    }
                }   
        }

    `

I’m definitely no expert and I’ve been trying to make this work for hours now… Can someone point me towards the right direction please?
Thank you,
Manue

Related posts

1 comment

  1. $metakey2 = 'jour';
    $jours = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey2) );
    
    // Chaeck the data returned isn't null, blank, 0-length array, etc.
    if (!empty($jours)) {
        // Initialize events array
        $events = array();
    
        // Loop through the data
        foreach ($jours as $jour) {
            $year  = substr($jour, 0, 4); // OUTPUT YEAR FROM DATE
            $month = substr($jour, 5, 2); // OUTPUT MONTH FROM DATE
            $day   = substr($jour, 7, 2); // OUTPUT DAY FROM DATE
    
            // PROCESS YOUR DATA HERE
            ...
        }
    }
    

    The key is the foreach loop after the empty check. You need to loop through the data that is returned from the db as an array.

Comments are closed.