Answer the question
In order to leave comments, you need to log in
How to display all products of a specific Woocommerce user on a website page or blog post?
Hello.
Tell me how to display all the products of a specific user (the one who added the product) Woocommerce on a website page or blog post?
There are many registered users on the site. Periodically, they add products to the site and write blog entries. How to display, for example, in the author's blog posts all the previously added goods?
Answer the question
In order to leave comments, you need to log in
Most likely, you want to display products not of a specific user, but of the current one. For this, a more hook may be suitable for you pre_get_posts
, on it, according to the necessary conditions, you can filter the main request:
add_action( 'pre_get_posts', 'set_products_current_user', 1 );
function set_products_current_user( $query ) {
// Выходим, если это админ-панель или не основной запрос
if( is_admin() || ! $query->is_main_query() )
return;
// Устанавливаем текущего юзера, если это запрос товаров
if ( $query->get( 'post_type' ) == 'product' ) {
$query->set( 'author', get_current_user_id() );
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question