I'm trying to hide products in Woocommerce according to the user ID who published them.
I have created the following code but it doesn't work well.
function Products_for_vendor() {
$args = array( 'post_type' => 'product', 'post_author' => '2' );
$products = get_posts( $args );
foreach ($products as $product->ID) {
$post_id = $product->ID
$terms = array( 'exclude-from-catalog', 'exclude-from-search' );
wp_set_object_terms( $post_id, $terms, 'product_visibility', false );
}
}
add_action( 'init', 'Products_for_vendor' );
to hide the post I extracted the code mentioned in this query: Change product visibility via PHP on Woocommerce 3+
Any help or comment is well received.
Thanks in advance.