Home:ALL Converter>How to use woocommerce_shortcode_products_orderby to order by custom date field

How to use woocommerce_shortcode_products_orderby to order by custom date field

Ask Time:2020-02-24T21:54:16         Author:kongen

Json Formatter

I have a specific WooCommerce category, which has an custom date field which is created by Advanced Custom Field.

I have an shortcode which outputs the products and i have found some code which is getting me a long way, but it does not register my field as an date, and does not order by that date.

The code I am using is:

add_filter( 'woocommerce_shortcode_products_query', 'woocommerce_shortcode_products_orderby' );

function woocommerce_shortcode_products_orderby( $args ) {



  $standard_array = array('menu_order','title','date','id', 'dato_for_anvendelse');

  if($args['tax_query'][1]['terms'][0] == 'sotilbud') {
    $args['meta_key'] = 'dato_for_anvendelse';
    $args['orderby']  = 'meta_value_num';
  }

 return $args;
}

How do I make the custom field "dato_for_anvendelse" into a date that is actually sortable?

I need to order by the custom field value which is an date and find the products closest to the current date.

Author:kongen,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60377387/how-to-use-woocommerce-shortcode-products-orderby-to-order-by-custom-date-field
yy