有没有办法让这个管理员产品仪表板只显示登录用户创建的产品?
我正在尝试 manage_{$post->post_type}_posts_custom_column
功能但不能移动太多
例如。我想要这样的东西
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
钩子(Hook)用于操作管理产品列表中的列内容。
所以你需要改变 产品查询从管理产品列表中使用:
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 文件中。测试和工作。
Now you will have only the products belonging to the current user ID, as we filter products by logged in author.
允许特定用户角色查看所有产品:
如果您想只允许“管理员”用户角色查看所有产品,您将在代码中插入
global $pagenow;
之后以下几行: // Allow administrator user roles
if( current_user_can( 'administrator' ) ) return;
关于php - 在 WooCommerce 管理产品列表中仅显示登录作者的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62153785/