我有一个供应商市场。我只想显示由特定作者用户角色(供应商)创建的产品。
澄清一下,我并不是想改变用户角色看到的内容。仅尝试显示供应商创建的产品。
I found 隐藏产品仅显示分配给 woocommerce 中某些用户角色的产品 https://stackoverflow.com/a/71320065/11987538应答码
但是我需要更改代码的哪一部分来添加作者用户角色?有什么建议吗?
要仅显示基于用户角色的产品,您可以使用woocommerce_product_query
动作挂钩
在我的回答中我使用get_users()
WordPress 函数,检索符合特定条件的用户列表。
一旦获得了属于用户角色的所有ID,我们就可以使用author__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 );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)