We have some event posted into wordpress which are ordered by date. All works well except when we enter an event for 2020 it comes at the top of the list rather than the bottom. The date is save in the database as "01/24/2020"
All 2019 event are ordered correctly, by day and then month, with next upcoming showing at top of list.
Below if the code for the query. I'm not sure if it the query where the issue is, the database or both. Appreciate any help or advice.
if( $data['items_per_page'] ) {
$event_perpage = $data['items_per_page'];
}
else {
$event_perpage = '10';
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'event',
'posts_per_page' => $event_perpage,
'paged' => $paged,
'meta_key' => $prefix.'event_date',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$wp_query = new WP_Query($args);
if($wp_query->have_posts()) :
while($wp_query->have_posts()) :
$wp_query->the_post(); ?>
<?php
// Get event date
$e_date = get_post_meta($post->ID, $prefix.'event_date', true);
$e_date_end = get_post_meta($post->ID, $prefix.'event_date_end', true);
if ( $e_date !== '' ) {
$event_date_string = $e_date;
$event_year = mysql2date( 'Y', $event_date_string );
$event_monthM = mysql2date( 'M', $event_date_string );
$event_day = mysql2date( 'd', $event_date_string );
$event_month = apply_filters('get_the_date', $event_monthM, 'M');
}
// If no date set
else {
$event_month = "No Date";
$event_day = "00";
$event_year = '';
}
if ( $e_date_end !== '' ) {
$event_date_end_string = $e_date_end;
$event_year_end = mysql2date( 'Y', $event_date_end_string );
$event_monthM_end = mysql2date( 'M', $event_date_end_string );
$event_day_end = mysql2date( 'd', $event_date_end_string );
$event_month_end = apply_filters('get_the_date', $event_monthM_end, 'M');
}
// Get event time
$e_time = get_post_meta($post->ID, $prefix.'event_time', true);
if ( $e_time !== '' ) { $event_time = $e_time; }
else { $event_time = __('No time set','qns'); }
// Get event location
$e_location = get_post_meta($post->ID, $prefix.'event_location', true);
if ( $e_location !== '' ) { $event_location = $e_location; }
else { $event_location = __('No location set','qns'); }
Thank you.
01
01/24/2020
is not a good format to store dates in, especially if you need to sort it. Store it as YYYYMMDD and it will sort numerically. So20200124
. Then change order by tometa_value_num
.function add_custom_scripts() { global $custom_meta_fields, $post; $output = '<script type="text/javascript"> jQuery(function() {'; foreach ($custom_meta_fields as $field) { // loop through the fields looking for certain types // date if($field['type'] == 'date') $output .= 'jQuery(".datepicker").datepicker();'; } $output .= '}); </script>'; echo $output; }
It seems like I need to edit the formatting of the Date display and how it's saved into the database