仅显示WooCommerce管理产品列表上登录作者的产品 [英] Display only products from the logged in author on WooCommerce admin products list
问题描述
有没有一种方法可以使此Admin产品仪表板仅显示由登录用户创建的产品?
Is there a way, by which this Admin product dashboard shows only the products created by the logged-in user?
我正在尝试使用 manage _ {$ post-> post_type} _posts_custom_column
函数,但动作不大
I am trying manage_{$post->post_type}_posts_custom_column
function but cannot move much
例如我想要这样的东西
add_action( 'manage_product_posts_custom_column', 'custom_column_content', 10, 2 );
function custom_column_content( $column, $product_id ){
if( logged in user==Product Author){
Display product;
}
else{
Dont display product
}
}
推荐答案
使用 manage_product_posts_custom_column
钩子可以操纵管理产品列表中的列内容.
The manage_product_posts_custom_column
hook is made to manipulate the columns content from admin products list.
因此,您需要使用以下方法从管理产品列表中更改产品查询:
So you need instead to alter the product query from admin products list using:
add_action( 'pre_get_posts', 'admin_pre_get_posts_product_query' );
function admin_pre_get_posts_product_query( $query ) {
global $pagenow;
// Targeting admin product list
if( is_admin() && 'edit.php' == $pagenow && isset($_GET['post_type']) && 'product' === $_GET['post_type'] ) {
$query->set( 'author', get_current_user_id() ); // Only displays the products created by the current user
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件中.经过测试,可以正常工作.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
现在,您将只拥有属于当前用户ID的产品,因为我们通过登录作者过滤产品.
Now you will have only the products belonging to the current user ID, as we filter products by logged in author.
允许特定的用户角色查看所有产品:
如果只允许管理员"用户角色查看所有产品,则将在代码中插入 global $ pagenow;
之后的几行:
If you want to allow only "administrator" user role to view all products, you will insert in the code, just after global $pagenow;
the following lines:
// Allow administrator user roles
if( current_user_can( 'administrator' ) ) return;
这篇关于仅显示WooCommerce管理产品列表上登录作者的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!