• Resolved Layson

    (@glennlaysonjr)


    I am using CMB2 fields for my custom post type event plugin. I am having a problem with the new posts showing ascending from year 2016, 2017 and then 2018. It will sort by month and day but not by year for some odd reason now. Anyone know what I am doing wrong.

    Here is the pre_get_posts query I have to customize the order:

    
    add_action( 'pre_get_posts', 'dc_sort_date' );
    function dc_sort_dates( $query ) {
        if ( $query->is_main_query() && !is_admin() ) {
            if ( $query->is_tax() || $query->is_post_type_archive('date_hub') ) {
                $query->set( 'posts_per_page', 6 );
                $query->set( 'meta_key', 'dc_datehub_dc_date' );
                $query->set( 'orderby', array('meta_value' => 'DESC'));
            }       
        }
    } 

    This is my date field:

    
     array(
    'name' => __( 'Event Date', 'date_hub' ),
    'id' => $prefix . 'dc_date',
    'desc' => 'Date - NOTE: Must Have to Display!',
    'type' => 'text_date',
    ),
    • This topic was modified 6 years, 10 months ago by Layson.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Just in case it’s playing a part, you reference the wrong callback in the code above. Your function is named “dc_sort_dates” and your add_action line references “dc_sort_date” without an S at the end.

    Otherwise, this is just a setup of some posts of a date_hub post type and viewing the term archive for a taxonomy assigned to that post type? It should show up to 6 posts, newest (2018) first, and move back through time in sequential order?

    Can you also verify what sort of values are being saved to your metadata? I’m curious if it’s not able to properly infer it as a date.

    Thread Starter Layson

    (@glennlaysonjr)

    Correct it was a typo on my part when I posted to the forum. I checked my script and it is matching.

    It is being saved using the text_date field that saves the data as 01/07/2018. I have checked this by displaying the selected date in the post and it shows all data.

    I also tested and added a date form 2016 and it show up before the 2017 posts. Not sure what all is happening.

    So as of right now its displaying the post by year going from oldest to newest but then in the months its being displayed within that year the newest month/day to the oldest.

    Any thoughts? @tw2113

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’ll need to set up my own instance for these details to try and confirm anything.

    As an alternative idea, it may be worth considering storing the data as UTC timestamps, which would very easily be sorted properly. There are field types available for that. However any existing posts would need to have the date re-saved to update the post meta.

    Nonetheless, added it to my todo for some debugging.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    So, I’ve done some tire kicking, and this is what I’ve come to a conclusion about.

    The issue at hand is not anything to do with CMB2 specifically. It’s an issue with dates having a tendency to suck, when it comes to dealing with programmatically.

    First up, I would recommend storing the actual meta value in a “Y/m/d” or very similar format. It does very much help with the process of ordering. The pre_get_posts filter you have looks solid and is matching what I have working locally.

    Regarding a possible different format for actual display for the date value, it’s easy enough to pass the meta value through the date() function + strtotime() to get a different version.

    $mydate = get_post_meta( get_the_ID(), 'dc_datehub_dc_date', true );
    echo date( 'M, d, Y', strtotime( $mydate ) ); 
    

    I’d consider it a worthwhile compromise if it means dates ordered correctly.

    Example CMB2 field for this, based on yours above.:

    $cmb->add_field( array(
    	'name' => __( 'Event Date', 'date_hub' ),
    	'id'   => $prefix . 'dc_date',
    	'desc' => 'Date - NOTE: Must Have to Display!',
    	'type' => 'text_date',
    	'date_format' => 'Y/m/d',
    ) );
    
    Thread Starter Layson

    (@glennlaysonjr)

    Worked great! Now I just have to have a script run in the plugin update to change the current metadata.

    Thanks!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Awesome. Thanks Layson

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘2018 Date Sorting Not Working’ is closed to new replies.