Woocommerce产品可由其作者针对特定用户角色进行编辑 [英] Woocommerce products editable by their author for specific user role

查看:63
本文介绍了Woocommerce产品可由其作者针对特定用户角色进行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WordPress网站上注册了具有不同角色的不同用户.除了其他用户外,我还希望允许广告客户(具有广告客户角色的用户-广告客户是我创建的自定义角色)将自己的产品放置在我的网站上并进行管理.但是只需要限制它们来管理(创建,编辑和删除)自己的产品,而不必管理其他产品.

I have different users registered on my WordPress website with different roles. Apart from the rest of users, I want to allow advertisers (users with advertiser role - advertiser is a custom role that I have created) to place their own products on my site and also manage them. But they need to be limited only to manage (create, edit and delete) their own products, not of others.

到目前为止,我已经尝试了以下代码,但它似乎无效.我敢肯定,我可以使用pre_get_posts操作来实现我的目标,以下函数可以为我提供帮助,但是在解决此代码问题时我需要一些帮助.我不确定产品的发布类型.

So far, I have tried the following code but it seems to be not valid. I am sure I can accomplish my goal using pre_get_posts action and the following function can help me but I need some help in resolving the issues with this code. I am not sure about the post type of products.

以下是我要实现目标的代码:

Here is the code that I am trying to accomplish my goal with:

function show_specific_advertiser_products( $query ) {
    $current_user = wp_get_current_user();
    if ( is_admin() && in_array ($query->get( 'post_type'), array( 'woocommerce_products' ) ) && !user_can( $current_user, 'administrator' ) ) {

        $query->set( 'author__in', $current_user->ID );
    }
}
add_action( 'pre_get_posts', 'show_specific_advertiser_products' );

任何帮助将不胜感激.

推荐答案

您的代码中的错误来自 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.

这篇关于Woocommerce产品可由其作者针对特定用户角色进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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