You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce前端仅显示当前登录用户的自有产品?

Restrict WooCommerce Frontend to Show Only Current User's Own Products

Hey there, sounds like you ran into a common issue where your previous code was too broad—blocking all non-user posts and pages instead of just limiting WooCommerce products. Let's fix that so logged-in users only see their own products, while leaving regular posts, pages, and admin functionality untouched.

How It Works

We'll use two key hooks to target WooCommerce specifically:

  1. Modify the main product archive query to only pull products created by the current logged-in user
  2. Restrict direct access to single product pages so users can't view products they didn't create

The Code

Add this to your theme's functions.php file, or use a custom plugin to keep it safe from theme updates:

add_action( 'pre_get_posts', 'restrict_products_to_current_user_frontend' );
function restrict_products_to_current_user_frontend( $query ) {
    // Only apply this to frontend product archives (shop, categories, tags)
    if ( ! is_admin() && $query->is_main_query() && ( is_post_type_archive( 'product' ) || is_tax( array( 'product_cat', 'product_tag' ) ) ) ) {
        if ( is_user_logged_in() ) {
            $current_user = get_current_user_id();
            // Filter query to only show products authored by the current user
            $query->set( 'author', $current_user );
        } else {
            // Optional: Redirect non-logged-in users to My Account page
            wp_redirect( wc_get_page_permalink( 'myaccount' ) );
            exit;
        }
    }
}

// Block access to single products not owned by the current user
add_action( 'template_redirect', 'restrict_single_product_to_owner' );
function restrict_single_product_to_owner() {
    if ( ! is_admin() && is_singular( 'product' ) ) {
        global $post;
        if ( is_user_logged_in() ) {
            $current_user = get_current_user_id();
            // If current user isn't the product author, redirect to 404
            if ( (int) $post->post_author !== $current_user ) {
                wp_redirect( get_404_template() );
                exit;
            }
        } else {
            wp_redirect( wc_get_page_permalink( 'myaccount' ) );
            exit;
        }
    }
}

Key Details & Adjustments

  • Admin Exclusion: The ! is_admin() check ensures admins and shop managers can still view and manage all products from the backend—no restrictions there.
  • Non-Logged-In Users: The code redirects guests to the My Account page, but you can remove that else block if you want guests to see a different message or no products at all.
  • Role Exceptions: If you want specific roles (like shop managers) to bypass this restriction, add a check with current_user_can(). For example, modify the logged-in condition to:
    if ( is_user_logged_in() && ! current_user_can( 'manage_woocommerce' ) )
    
  • Testing: Always test with multiple user accounts—log in as a regular user to confirm only their products show up, and as an admin to ensure full access is preserved.

内容的提问来源于stack exchange,提问作者Marko I.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:50:44