更改 woocommerce 仪表板屏幕中的默认排序顺序 [英] Change default sorting order in woocommerce dashboard screen

查看:33
本文介绍了更改 woocommerce 仪表板屏幕中的默认排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 woocommerce 管理屏幕中的默认排序顺序.

I am trying to change the default sorting order in woocommerce admin screen.

我正在尝试使用以下代码默认按产品名称对产品页面进行排序.

I am trying the following code to sort the product page by product name by default.

/* Sort posts in wp_list_table by column in ascending or descending order. */
function custom_post_order($query){
    /* 
        Set post types.
        _builtin => true returns WordPress default post types. 
        _builtin => false returns custom registered post types. 
    */
    $post_types = get_post_types(array('_builtin' => false), 'names');
    /* The current post type. */
    $post_type = $query->get('post_type');
    /* Check post types. */
    if(in_array($post_type, $post_types) && $post_type == 'product'){
        /* Post Column: e.g. title */
        if($query->get('orderby') == ''){
            $query->set('orderby', 'title');
        }
        /* Post Order: ASC / DESC */
        if($query->get('order') == ''){
            $query->set('order', 'ASC');
        }
    }
}
if(is_admin()){
    add_action('pre_get_posts', 'custom_post_order');
}

但它似乎不起作用.谁能指出我哪里出错了,或者 woocommerce 对产品列的处理方式不同.

But it does not seems to be working. Can anyone please point where I am going wrong Or does woocommerce handles product columns differently.

推荐答案

我会以 parse_query 为目标.这似乎对我有用:

I would target parse_query instead. This seems to be working for me:

/* Sort products in wp_list_table by column in ascending or descending order. */
function sp_41964737_custom_product_order( $query ){

    global $typenow;

    if( is_admin() && $query->is_main_query() && $typenow == 'product' ){

        /* Post Column: e.g. title */
        if($query->get('orderby') == ''){
            $query->set('orderby', 'title');
        }
        /* Post Order: ASC / DESC */
        if($query->get('order') == ''){
            $query->set('order', 'ASC');
        }

    }
}
add_action( 'parse_query', 'sp_41964737_custom_product_order' );

结果:

这篇关于更改 woocommerce 仪表板屏幕中的默认排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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