【问题标题】:Change default sorting order in woocommerce dashboard screen在 woocommerce 仪表板屏幕中更改默认排序顺序
【发布时间】:2017-01-31 18:19:29
【问题描述】:

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

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

/* 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处理产品列的方式不同。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

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

    /* 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' );
    

    结果:

    【讨论】:

    • 请尝试禁用其他插件并切换到默认主题,以确保您没有任何冲突。我真的非常喜欢这个结果,我会把它保留在我的测试环境中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2019-02-17
    • 1970-01-01
    相关资源
    最近更新 更多