首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Woocommerce可排序列不起作用

Woocommerce可排序列不起作用
EN

Stack Overflow用户
提问于 2015-12-16 12:17:46
回答 1查看 961关注 0票数 0

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

代码语言:javascript
复制
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进行排序,两者看起来都是随机的,但都是一致的。请记住,这两个字段都是自定义字段,可能有也可能没有定义值。我怎么才能把这个分类呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-16 13:39:31

下面是关于自定义可排序列的一个很好的教程。注册列后,需要处理实际的排序。可悲的是,这部分并不是自动发生的。未经测试,但改编自上述教程:

代码语言:javascript
复制
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;

      }

   }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34311840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档