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

问题描述 投票:1回答:1

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

到目前为止,我已经尝试了以下代码,但它似乎无效。我确信我可以使用pre_get_posts操作完成我的目标,以下功能可以帮助我,但我需要一些帮助来解决此代码的问题。我不确定邮政类型的产品。

以下是我试图通过以下方式实现目标的代码:

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

任何帮助将非常感谢。

wordpress woocommerce product custom-post-type
1个回答
1
投票

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

所以请尝试以下方法:

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文件中。它应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.