0

How I want to sort my custom column contain string date with format j F Y

my column data contains string example "21 januari 2021", how I can change the format to date so I sort it.

My value from postmeta with meta_key "datepicker".

here my code:

    add_action( 'pre_get_posts', 'manage_wp_posts_be_qe_pre_get_posts', 1 );
function manage_wp_posts_be_qe_pre_get_posts( $query ) {


   if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {

      switch( $orderby ) {

     
         case 'delivery_date':

            // set our query's meta_key, which is used for custom fields
            $query->set( 'meta_key', 'datepicker' );

            $query->set( 'orderby', 'meta_value' );
            break;

      }

   }

}

how I can orderby meta_valuewith format date?

2
  • 1
    You cannot compare dates with something like"21 januari 2021". But you could reformat your date. You can compare/sort date-strings with a format like Y-m-d.
    – LWS-Mo
    Jan 20 at 11:40
  • As a rule of thumb, you should store date in the database either as unix timestamp or with the correct column type. As you cannot set the latter for WordPress, I'd always save dates as unix timestamp and do the formatting then later.
    – kero
    Jan 20 at 13:20

Your Answer

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

Browse other questions tagged or ask your own question.