根据 WooCommerce 管理产品列表中的用户角色仅显示待处理产品

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

我想在 WooCommerce 管理产品列表中仅显示商店经理待处理的产品并隐藏所有垃圾

使用CSS我能够部分隐藏我不想看到的项目:

.current,
.draft,
.publish,
.search-box,
.byorder,
.tablenav.top,
    .page-title-action {
display: none;
    visibility:hidden;
}

这还不够,所以我还使用:

function exclude_other_author_products($query) {
  $current_user = wp_get_current_user();
  if (in_array('administrator', $current_user->shop_manager)
    return $query;
if ($query->query['post_type'] == 'product' && $query->is_main_query()) {
    $query->set('author__in', $current_user->ID);
}
 }

add_action('pre_get_posts', 'exclude_other_author_products');

但是,这会产生一个严重错误:

syntax error, unexpected token "return"

有什么建议吗?

php wordpress woocommerce backend user-roles
1个回答
1
投票

您可以使用

post_status

  • publish
    - 已发布的帖子或页面
  • pending
    - 帖子正在等待审核
  • draft
    - 处于草稿状态的帖子
  • auto-draft
    - 新创建的帖子,没有内容
  • future
    - 将来发布的帖子
  • private
    - 未登录的用户不可见
  • inherit
    - 修订版。请参阅 get_children。
  • trash
    - 帖子在垃圾箱中。

注意: 用户角色和帖子状态都由一个数组组成。所以可以添加几个,用逗号分隔

所以你得到:

function action_pre_get_posts( $query ) {   
    global $pagenow, $post_type;
    
    // Targeting admin product list
    if ( $query->is_admin && $pagenow === 'edit.php' && $post_type === 'product' ) {
        // Get current user
        $user = wp_get_current_user();
    
        // Roles
        $roles = (array) $user->roles;
        
        // Roles to check
        $roles_to_check = array( 'shop_manager' );
        
        // Compare
        $compare = array_diff( $roles, $roles_to_check );
    
        // Result is empty
        if ( empty ( $compare ) ) {
            // Set "post status"
            $query->set( 'post_status', array( 'pending' ) );
            
            /* OPTIONAL
            // Set "posts per page"
            $query->set( 'posts_per_page', 20 );

            // Set "paged"
            $query->set( 'paged', ( get_query_var('paged') ? get_query_var('paged') : 1 ) );
            */
        }
    }
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );
© www.soinside.com 2019 - 2024. All rights reserved.