admin管理员组文章数量:1026989
I have this code where the authors can see only own posts. I need the authors to see only the products they have created on woocommerce. I know the woocommerce products have no authors by default, but I can add this with a plugin. Whats I want to do is to change this snippet to limit the lists of products in the custom post type of woocommerce.
Someone know the best way to do that?
Thanks!
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'manage_options' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
I have this code where the authors can see only own posts. I need the authors to see only the products they have created on woocommerce. I know the woocommerce products have no authors by default, but I can add this with a plugin. Whats I want to do is to change this snippet to limit the lists of products in the custom post type of woocommerce.
Someone know the best way to do that?
Thanks!
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'manage_options' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
Share
Improve this question
asked Mar 20, 2019 at 20:21
Fernando AurelianoFernando Aureliano
1032 bronze badges
0
1 Answer
Reset to default 1I believe changing the following conditional:
if( !current_user_can( 'manage_options' ) )
to this one:
if( !current_user_can( 'manage_options' ) && 'product' === $query->get( 'post_type' ) )
would do it.
Brief explanation: I added a second condition which checks if the query is for WooCommerce products where the post type is product
.
UPDATE
If you want to make certain that the code runs only on the products page (WooCommerce → Products), then you can (remove the global $pagenow;
and) change this:
if( 'edit.php' != $pagenow || !$query->is_admin )
to this one:
if( !$query->is_admin || 'edit-product' !== get_current_screen()->id )
And instead of relying on the global $user_ID
, you should probably use get_current_user_id()
: $query->set( 'author', get_current_user_id() );
:)
I have this code where the authors can see only own posts. I need the authors to see only the products they have created on woocommerce. I know the woocommerce products have no authors by default, but I can add this with a plugin. Whats I want to do is to change this snippet to limit the lists of products in the custom post type of woocommerce.
Someone know the best way to do that?
Thanks!
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'manage_options' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
I have this code where the authors can see only own posts. I need the authors to see only the products they have created on woocommerce. I know the woocommerce products have no authors by default, but I can add this with a plugin. Whats I want to do is to change this snippet to limit the lists of products in the custom post type of woocommerce.
Someone know the best way to do that?
Thanks!
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'manage_options' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
Share
Improve this question
asked Mar 20, 2019 at 20:21
Fernando AurelianoFernando Aureliano
1032 bronze badges
0
1 Answer
Reset to default 1I believe changing the following conditional:
if( !current_user_can( 'manage_options' ) )
to this one:
if( !current_user_can( 'manage_options' ) && 'product' === $query->get( 'post_type' ) )
would do it.
Brief explanation: I added a second condition which checks if the query is for WooCommerce products where the post type is product
.
UPDATE
If you want to make certain that the code runs only on the products page (WooCommerce → Products), then you can (remove the global $pagenow;
and) change this:
if( 'edit.php' != $pagenow || !$query->is_admin )
to this one:
if( !$query->is_admin || 'edit-product' !== get_current_screen()->id )
And instead of relying on the global $user_ID
, you should probably use get_current_user_id()
: $query->set( 'author', get_current_user_id() );
:)
本文标签: pluginsWoocommerce limit user to see only the products he created
版权声明:本文标题:plugins - Woocommerce: limit user to see only the products he created 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745670194a2162411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论