D
D
DimDim77782021-08-20 15:48:09
WordPress
DimDim7778, 2021-08-20 15:48:09

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

2 answer(s)
A
Artem Zolin, 2021-08-21
@DimDim7778

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() );
  }

}

A
Alexander, 2021-08-20
@apisklov

$query = new WP_Query( 'post_type=product&author=123' );
while ( $query->have_posts() ) {
  $query->the_post();

  the_title();
}

123-
WP_Query user id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question