根据 WooCommerce 管理员产品列表中的用户角色仅显示待定产品
Display only pending products based on user role in WooCommerce admin product list
我想在商店经理的 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"
有什么建议吗?
您可以使用post_status
publish
- 已发布 post 或页面
pending
- post 待审核
draft
- post 处于草稿状态
auto-draft
- 新创建的 post,没有内容
future
- 一个post以后发布
private
- 未登录的用户不可见
inherit
- 修订版。参见 get_children。
trash
- post 在垃圾箱中。
注意:用户角色和post状态都由一个数组组成。所以可以加几个,用逗号隔开
所以你得到:
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 );
我想在商店经理的 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"
有什么建议吗?
您可以使用post_status
publish
- 已发布 post 或页面pending
- post 待审核draft
- post 处于草稿状态auto-draft
- 新创建的 post,没有内容future
- 一个post以后发布private
- 未登录的用户不可见inherit
- 修订版。参见 get_children。trash
- post 在垃圾箱中。
注意:用户角色和post状态都由一个数组组成。所以可以加几个,用逗号隔开
所以你得到:
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 );