1

i want to get 10 product from a category in woocommerce

for example, for get latest post of a posts category i use the following code

<?php $posts = get_posts( 'category=17&numberposts=5' ); ?>
<?php if( $posts ) : ?>
    <ul>
        <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
            <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><i class="circle"></i><?php the_title(); ?></a></li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

i want a code, like this for get woocommerce products

Nicolas Cami
  • 458
  • 1
  • 4
  • 13
Bahadori
  • 129
  • 1
  • 4
  • 14

3 Answers3

0

try this example :

$args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'ignore_sticky_posts'   => 1,
    'posts_per_page'        => '12',
    'meta_query'            => array(
        array(
            'key'           => '_visibility',
            'value'         => array('catalog', 'visible'),
            'compare'       => 'IN'
        )
    ),
    'tax_query'             => array(
        array(
            'taxonomy'      => 'product_cat',
            'field'         => 'term_id', //This is optional, as it defaults to 'term_id'
            'terms'         => 26,
            'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
        )
    )
);
$products = new WP_Query($args);

/* your loop */

Hope this will helps you.

Mostafa Norzade
  • 1,578
  • 5
  • 24
  • 40
-1

Try this example,

<?php  
$args = array(
    'post_type'      => 'product',
    'posts_per_page' => 10,
    'product_cat'    => 'hoodies'
);

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();
    global $product;
    echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;

wp_reset_query();
?>

OR

<?php
$args     = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
$products = get_posts( $args ); 
?>

Hope this will helps you.

For more details please visit, Woocommerce get products

Sunil Dora
  • 1,407
  • 1
  • 13
  • 26
  • thank you, its working:) but this code not display products price and "add to cart" link, how to display this items? – Bahadori May 28 '18 at 07:53
  • check this link https://stackoverflow.com/questions/36380749/how-to-get-all-product-in-the-woocommerce-programatically – Sunil Dora May 28 '18 at 07:59
  • If this is working please apply the correct answer to this. So this will help others. – Sunil Dora May 28 '18 at 08:00
  • and if you're displaying the products with custom way, you need to add code display the things you need. – Sunil Dora May 28 '18 at 08:19
-2

why not use woocommerce's product_category shortcode?

<?php echo do_shortcode("[product_category category='17' limit='5']"); ?>

It will list the products same as in shop page. With these you are safe with product attributes like if when it's out of stock.

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139