0

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.

5
  • please use simple-custom-post-order plugin Sep 27, 2019 at 8:52
  • Thank you for this suggestion however I would rather have the correct query than use a plugin ;-)
    – Tom Mills
    Sep 27, 2019 at 8:57
  • It’s appearing first because you’re sorting alphabetically and it begins with 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. So 20200124. Then change order by to meta_value_num. Sep 27, 2019 at 11:33
  • OK thank you, will try this today. Have inherited the site so trying to unpick somebody else's work, never easy ;-)
    – Tom Mills
    Oct 1, 2019 at 8:50
  • Looks to be the jquery datepicker that is causing the issue. The datepicker is called using following: 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
    – Tom Mills
    Oct 1, 2019 at 9:52

1 Answer 1

0

OK so fixed this by updating the following line from

$output .= 'jQuery(".datepicker").datepicker();';

to

$output .= 'jQuery(".datepicker").datepicker({ dateFormat: "yy-mm-dd" });';

Thank you for all the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.