Support

Account

Home Forums Backend Issues (wp-admin) Sorting by Date field in Woocommerce Orders Admin view Reply To: Sorting by Date field in Woocommerce Orders Admin view

  • I am looking for some assistance with the following:

    Currently I have a custom function setup to display my ACF field “start_date_dont_edit” as a column on my backend Orders dashboard in Woocommerce. The column shows, allows sorting, and displays my date field values perfectly. However, it is not sorting correctly by date in either direction. It just seems kind of random.

    Hoping someone might be able to have a look and see what I am missing or did wrong, in order to get the column to sort by date (oldest to newest and vice versa) when clicking on the sortable column.

    Thanks!

    // Order column - Add ACF Start Date to Admin view
    function webroom_add_order_new_column_header( $columns ) {
    
        $new_columns = array();
    
        foreach ( $columns as $column_name => $column_info ) {
    
            $new_columns[ $column_name ] = $column_info;
    
            if ( 'order_total' === $column_name ) {
                $new_columns['order_details'] = __( 'ACF Start', 'acf-start' );
            }
        }
    
        return $new_columns;
    }
    add_filter( 'manage_edit-shop_order_columns', 'webroom_add_order_new_column_header', 20);
    
    /* Add data in column to sort */
    add_action( 'manage_shop_order_posts_custom_column', 'webroom_add_wc_order_admin_list_column_content' );
     
    function webroom_add_wc_order_admin_list_column_content( $column ) {
       
        global $post;
     
        if ( 'order_details' === $column ) {
     
            $order = wc_get_order( $post->ID );
    		$thisacof6 = get_post_meta( $order->get_id(), 'start_date_dont_edit', true );
    		$new_startdate = date_format(date_create_from_format('Y-m-d', $thisacof6),'m-d-y');
            echo $new_startdate;
          
        }
    }
    
    // Make custom column sortable ** Need help figuring out how to get the column to sort by date
    add_filter( "manage_edit-shop_order_sortable_columns", 'shop_order_column_meta_field_sortable' );
    function shop_order_column_meta_field_sortable( $columns )
    {
        $meta_key = 'start_date_dont_edit';
        return wp_parse_args( array('order_details' => $meta_key), $columns );
    }
    
    // Make sorting work properly (by numerical values)
    add_action('pre_get_posts', 'shop_order_column_meta_field_sortable_orderby' );
    function shop_order_column_meta_field_sortable_orderby( $query ) {
        global $pagenow;
    
        if ( 'edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ){
    
            $orderby  = $query->get( 'orderby');
            $meta_key = 'start_date_dont_edit';
    
            if ('start_date_dont_edit' === $orderby){
              $query->set('meta_key', $meta_key);
              $query->set('orderby', 'meta_value_num');
            }
        }
    }