且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Woocommerce产品可由其作者针对特定用户角色进行编辑

更新时间:2023-11-30 11:49:58

您的代码中的错误来自 post_type ...对于woocommerce产品,它只是 product .您必须用自定义用户角色替换管理员.

The error in your code comes from the post_type… for woocommerce products it's simply product. You will have to replace administrator by your custom user role.

因此,请尝试以下操作:

So try the following instead:

add_action( 'pre_get_posts', 'show_specific_advertiser_products' );
function show_specific_advertiser_products( $query ) {
    $user = wp_get_current_user();
    if ( is_admin() && $query->get( 'post_type') === 'product' && in_array('administrator', $user->roles) ) {
        $query->set( 'author', $user->ID );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.应该可以.

Code goes in function.php file of your active child theme (or active theme). It should works.