在 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

        }

}


墨色风雨
浏览 85回答 1
1回答

烙印99

挂钩manage_product_posts_custom_column用于操作管理产品列表中的列内容。因此,您需要改为使用以下方法从管理产品列表中更改产品查询: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文件。测试和工作。现在您将只有属于当前用户 ID 的产品,因为我们按登录作者过滤产品。允许特定用户角色查看所有产品:如果您只想允许“管理员”用户角色查看所有产品,您将在代码中插入global $pagenow;以下几行:    // Allow administrator user roles    if( current_user_can( 'administrator' ) ) return;
打开App,查看更多内容
随时随地看视频慕课网APP