php - 在 WooCommerce 管理产品列表中仅显示登录作者的产品

标签 php wordpress woocommerce product hook-woocommerce

有没有办法让这个管理员产品仪表板只显示登录用户创建的产品?

我正在尝试 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/

相关文章:

Wordpress 登录错误 - 上传文件夹必须是可写的

wordpress - 添加优惠券代码(wordpress)后如何刷新/重新加载 WooCommerce 结账页面?

mysql - 如何编辑wordpress wp_option表值

php - 如何显示 Woocommerce 类别描述

php - 从 WooCommerce 中的自定义字段调用值时如何使用 jQuery 删除类

php - 在 PHP 中上传 30 多个文件

php - 在 Drupal 7 中获取提交的帖子值

php - iOS 安全性将带有密码的数据发送至服务器或从服务器发送数据

php - php 中的安全首选项、php 函数来清理输入

php - 根据购物车总计(小计 + 运费)计算费用,而不将其添加到 WooCommerce 中的订单总值(value)