如何在管理页面中添加可排序的日期列?

时间:2022-02-18 作者:Sasha Grievus

我正在尝试向admin表中添加一个日期列(具体来说,是为了woocommerce订单页面,但此代码不依赖于插件)。该代码用于添加列,但每当我尝试点击订单页面上的排序三角形按钮对列进行排序时,它都会返回“找不到记录”。返回日期的函数中的日期格式可能有问题?代码的其余部分相当标准

// Adding a new column to ADMIN order list
add_filter( \'manage_edit-shop_order_columns\', \'custom_shop_order_column\', 20 );
function custom_shop_order_column($columns)
{
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  \'order_status\' ){
            // Inserting after "Status" column
            $reordered_columns[\'closing_date_column\'] = __( \'Validación\',\'theme_domain\');
        }
    }
    return $reordered_columns;
}

// Adding custom fields meta data for new column in ADMIN order list
// to add closing date to order solicitado passed to en espera (being so closed)
add_action( \'manage_shop_order_posts_custom_column\' , \'custom_orders_list_column_content\', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    switch ( $column )
    {
        case \'closing_date_column\' :
            echo calculateClosingDate($post_id);
            break;
    }
}

// Make custom column sortable
add_filter( "manage_edit-shop_order_sortable_columns", \'shop_order_column_meta_field_sortable\' );
function shop_order_column_meta_field_sortable( $columns )
{
    $meta_key = \'closing_date_column\';
    return wp_parse_args( array(\'closing_date_column\' => $meta_key), $columns );
}

// Make sorting work properly 
add_action(\'pre_get_posts\', \'shop_order_column_meta_field_sortable_orderby\' );
function shop_order_column_meta_field_sortable_orderby( $query ) {
    global $pagenow;

    $orderby  = $query->get( \'orderby\');
    if (\'closing_date_column\' === $orderby){
        $query->set(\'meta_key\',\'closing_date_column\');
        $query->set(\'orderby\', \'meta_value_date\');
    }    
}
// Adding a new column to ADMIN order list
function calculateClosingDate($post_id){
    // doing stuff
}
编辑:我认为问题可能在于自定义表中显示的数据是计算出来的,而不是存储在db上的元键值中,因此每当它尝试排序时,都会在db上搜索不存在的数据。那么,我如何对计算出的数据进行排序呢?

1 个回复
最合适的回答,由SO网友:Sasha Grievus 整理而成

对于路过的人来说,把所有这些代码放在一起并不是一件小事。问题实际上是缺乏数据。我解决了将数据添加为元键(在我的情况下,当订单状态改变时添加)并将其作为列值进行响应的问题。因此,完整代码是:

// Adding a new column to ADMIN order list
add_filter( \'manage_edit-shop_order_columns\', \'custom_shop_order_column\', 20 );
function custom_shop_order_column($columns)
{
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  \'order_status\' ){
            // Inserting after "Status" column
            $reordered_columns[\'closing_date_column\'] = __( \'Validación\',\'theme_domain\');
        }
    }
    return $reordered_columns;
}

// Echoing custom fields meta data for new column in ADMIN order list
add_action( \'manage_shop_order_posts_custom_column\' , \'custom_orders_list_column_content\', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    switch ( $column )
    {
        case \'closing_date_column\' :
            echo get_post_meta( $post_id, \'closing_date_column\', true );
            break;
    }
}

// Make custom column sortable
add_filter( "manage_edit-shop_order_sortable_columns", \'shop_order_column_meta_field_sortable\' );
function shop_order_column_meta_field_sortable( $columns )
{
    $meta_key = \'closing_date_column\';
    return wp_parse_args( array(\'closing_date_column\' => $meta_key), $columns );
}

// Make sorting work properly for date field
add_action(\'pre_get_posts\', \'shop_order_column_meta_field_sortable_orderby\' );
function shop_order_column_meta_field_sortable_orderby( $query ) {
    global $pagenow;

    $orderby  = $query->get( \'orderby\');
    if (\'closing_date_column\' === $orderby){
        $query->set(\'meta_key\',\'closing_date_column\');
        $query->set(\'orderby\', \'meta_value_date\');
    }    
}

// add closing date to order meta when it changes status
// The format is "Y-m-d" for sorting doesn\'t work with any date format
add_action( \'woocommerce_order_status_changed\', \'action_woocommerce_order_status_changed\', 10, 4 ); 
function action_woocommerce_order_status_changed( $this_get_id, $this_status_transition_from, $this_status_transition_to, $instance ) { 
    update_post_meta( $this_get_id, \'closing_date_column\', date("Y-m-d") );
}; 

相关推荐

Table block - Wrap table

我正在使用古腾堡创建表格。古腾堡表的前端结构如下:<figure class="wp-block-table"> <table> ... </table> </figure> 我想给我的桌子添加阴影,但由于overflow-x : auto 在父元素上figure.一种修复方法是像这样包装表:<figure class="wp-block-table&quo