Woocommerce可排序列不起作用 [英] Woocommerce sortable columns not working

查看:72
本文介绍了Woocommerce可排序列不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下方法在WordPress管理员的 Woocommerce 订单列表中添加了几个自定义字段列,但排序不起作用....

I've added a couple of custom field columns to our Woocommerce orders list in the admin of WordPress using the methods below, but the sort is not working....

add_filter( 'manage_edit-shop_order_columns', 'my_wc_columns' );
function my_wc_columns($columns){
    $new_columns = (is_array($columns)) ? $columns : array();
    unset( $new_columns['order_actions'] );
    $new_columns['program_id'] = 'Program';
    $new_columns['constituent_id'] = 'Constituent ID';
    $new_columns['order_actions'] = $columns['order_actions'];
    return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'my_wc_column_values', 2 );
function my_wc_column_values($column){
    global $post;
    if ( $column == 'program_id' ) {
        $program = get_post_meta( $post->ID, '_program_id', true );
        $program_title = get_the_title($program);
        $column_val = (isset($program) && $program>0 ? $program_title : 'All');
        echo '<span>' . my_programs_get_name( $column_val ) . ' (' . $program . ')</span>';
    }
    if ( $column == 'constituent_id' ) {
        $consid = get_post_meta( $post->ID, 'constituent_id', true );
        $column_val = (isset($consid) && $consid != "") ? $consid : "";
        echo '<span>' . $column_val . '</span>';
    }
}
// Make column sortable
add_filter( "manage_edit-shop_order_sortable_columns", 'my_wc_column_sort' );
function my_wc_column_sort( $columns ) {
    $custom = array(
        'program_id'    => '_program_id',
        'constituent_id'    => 'constituent_id',
    );
    return wp_parse_args( $custom, $columns );
}

我希望程序名称可能有问题,因为它是一个ID,需要通过自定义函数将其转换为名称,但是没有一列可以正确排序.单击记录的列标题后,记录会更改顺序,但是我无法确定排序的方式.该程序未按名称或ID进行排序,并且两者似乎都是随机的,但保持一致.请记住,这两个字段都是自定义字段,可能没有定义值.如何使它可排序?

I expected to have an issue perhaps with the program name, since it is an id that needs to be translated via a custom function to a name, but neither column is sorting properly. The records change order after clicking their column titles, but I cannot tell how the sort is being done. The program is not sorting on name or ID and both are seem random but consistent. Keep in mind both fields are custom fields that may or may not have a value defined. How can I make this sortable?

推荐答案

关于

Here's a good tutorial on custom sortable columns. After you register the column, you need to handle the actual sorting. Sadly, that part doesn't happen automagically. Untested, but adapted from the above tutorial:

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

   /**
    * We only want our code to run in the main WP query
    * AND if an orderby query variable is designated.
    */
   if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {

      switch( $orderby ) {

         // If we're ordering by 'program_id'
         case 'program_id':

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

            /**
             * Tell the query to order by our custom field/meta_key's
             * value
             *
             * If your meta value are numbers, change 'meta_value'
             * to 'meta_value_num'.
             */
            $query->set( 'orderby', 'meta_value' );

            break;

      }

   }

}

这篇关于Woocommerce可排序列不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆