Get Products by Category id

i am writing a pricing table plugin for woocommerce. the user inserts the shortcode with an id of woocommerce products category and after updating the page the user can see a table with a list of product’s names and prices.
how can i get a list of products with the id of their category?!
in the code below $pid is what the user type in shortcode, ‘object_id’ is the id of the every product in wp_posts table.

<?php
    $products = $wpdb->get_results("SELECT object_id FROM {$wpdb->term_relationships}
                                      WHERE term_taxonomy_id = " . $pid);
    if(!empty($products))
    {
        foreach($products as $product)
        {
            //the code
        }
    }
?>  

Thanks in advance.

Related posts

Leave a Reply

5 comments

  1.  $args = array(
        'post_status' => 'publish',
        'tax_query' => array(
           array(
             'taxonomy' => 'product_cat',
             'field'    => 'term_id',
             'terms'     =>  '[ category id here ]', // When you have more term_id's seperate them by komma.
             'operator'  => 'IN'
             )
           )
        );
        $the_query = wp_query($args);
    

    Untested but should work

  2. By using the get_posts wordpress function

    Get All WooCommerce Product Details by Category

    $all_products = get_posts( array(
        'post_type' => 'product',
        'numberposts' => -1,
        'post_status' => 'publish',
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'your_product_category', /*category name*/
                'operator' => 'IN',
                )
            ),
        ));
        echo var_dump($all_products);
    

    Get All WooCommerce Product IDs by Category

    $all_ids = get_posts( array(
      'post_type' => 'product',
      'numberposts' => -1,
      'post_status' => 'publish',
      'fields' => 'ids',
      'tax_query' => array(
          array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => 'your_product_category', /*category name*/
              'operator' => 'IN',
              )
           ),
       ));
       foreach ( $all_ids as $id ) {
           echo $id;
       }
    

    Tested and its working fine.

  3. According to Woocommerce documentation WP_Query() or get_posts() should not be used:

    wc_get_products and WC_Product_Query provide a standard way of
    retrieving products that is safe to use and will not break due to
    database changes in future WooCommerce versions. Building custom
    WP_Queries or database queries is likely to break your code in future
    versions of WooCommerce as data moves towards custom tables for better
    performance. This is the best-practices way for plugin and theme
    developers to retrieve multiple products. wc_get_products and
    WC_Product_Query are similar to WordPress get_posts and WP_Query. Just
    like those, you pass in an array of arguments defining the criteria
    for the search.

    WooCommerce Docs

    Here’s my solution:

    $product_term_ids = array(16,10,4,7);
    
    $product_term_args = array(
        'taxonomy' => 'product_cat',
        'include' => $product_term_ids,
        'orderby'  => 'include'
    );
    $product_terms = get_terms($product_term_args);
    
    $product_term_slugs = [];
    foreach ($product_terms as $product_term) {
        $product_term_slugs[] = $product_term->slug;
    }
    
    $product_args = array(
        'post_status' => 'publish',
        'limit' => -1,
        'category' => $product_term_slugs,
        //more options according to wc_get_products() docs
    );
    $products = wc_get_products($product_args);
    
    foreach ($products as $product) {
        echo $product->get_title();
    }
    

    Note: category argument requires an array of slugs, not IDs.

    (Tested and works with WordPress 5.9.3 & WooCommerce 6.4.1.)

    Credit: @Christian Lescuyer

  4. The above (accepted) answer didn’t work (more results than there should have been) for me (version 4.9.8)

     'tax_query' => array(
         array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'     =>  [$request['id']],
            'operator'  => 'IN'
          )
      )
    

    The above works. I’m not really a WordPress person but I’d guess it’s a version thing…

  5. $posts = get_posts(
        array(
           'post_type'   => 'product',
            'numberposts' => -1,
            'post_status' => 'publish',
            'fields'      => 'ids',
            'tax_query'   => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'slug',
                    'terms'    => 'books-and-handbooks',
                    'operator' => 'IN',
                ),
            ),
        )
    );
    $arr = array_slice( $all_ids, 0, 50 );