1
votes

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

3

3 Answers

0
votes

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.

-1
votes

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

-2
votes

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.