仅根据作者在WooCommerce中的前端中的用户角色显示产品

发布于 2025-01-29 09:04:24 字数 251 浏览 2 评论 0原文

我有一个供应商市场。我只想显示由某个作者用户角色(供应商)创建的产品。

为了澄清,我不是要更改用户角色所看到的。仅尝试显示由供应商创建的产品。

我发现隐藏产品仅显示给WooCommerce中某些用户角色分配的产品答案代码,

但是我可以将代码的哪一部分更改为哪个部分添加作者用户角色?有建议吗?

I have a vendor marketplace. I’d like to only display products that was created by a certain author user role (vendor).

To clarify, I’m not trying to change what user role sees what. Only trying to display products which are created by vendor.

I found Hide products only show assigned products to certain user roles in woocommerce answer code

But what part of the code would I change to add the author user role? Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2025-02-05 09:04:24

要仅根据用户角色显示产品,您可以使用woocommerce_product_query

在我的答案中,我使用get_users() wordpress函数,检索与某些标准匹配的用户列表。

一旦获得了属于用户角色的所有ID,我们就可以使用wuter__in显示与某些作者相关的帖子,

因此您可以得到:

// Change the shop query
function action_woocommerce_product_query( $q, $query ) {
    // NOT on backend
    if ( is_admin() ) return;

    // Get IDs belonging to certain user roles - multiple roles can be added, separated by a comma
    $ids = get_users( array( 'role__in' => array( 'subscriber', 'administrator', 'vendor' ), 'fields' => 'ID' ) );
    
    // Show posts associated with certain author - use author id
    $q->set( 'author__in', $ids );
}
add_action( 'woocommerce_product_query', 'action_woocommerce_product_query', 10, 2 );

To display only products based on user roles you can use the woocommerce_product_query action hook

In my answer I use the get_users() WordPress function, to retrieve a list of users matching certain criteria.

Once all IDs belonging to user roles have been obtained, we can use author__in to show posts associated with certain author(s)

So you get:

// Change the shop query
function action_woocommerce_product_query( $q, $query ) {
    // NOT on backend
    if ( is_admin() ) return;

    // Get IDs belonging to certain user roles - multiple roles can be added, separated by a comma
    $ids = get_users( array( 'role__in' => array( 'subscriber', 'administrator', 'vendor' ), 'fields' => 'ID' ) );
    
    // Show posts associated with certain author - use author id
    $q->set( 'author__in', $ids );
}
add_action( 'woocommerce_product_query', 'action_woocommerce_product_query', 10, 2 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文