Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Woocommerce - Showing last 5 products

Featured Replies

Hey All,

 

I have the following code that shows the last 5 products for the shop:

h1>Recently Added</h1>

<ul class="row-fluid">

    <?php
        $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

                <li class="span3">    

                    <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

                        <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>

                        <h3><?php the_title(); ?></h3>

                           <span class="price"><?php echo $product->get_price_html(); ?></span>

                    </a>

                    <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
                </li><!-- /span3 -->
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

</ul><!-- /row-fluid -->

which is fine now I want to alter this slightly so that it shows the last 5 products for a vendor ( as i have a vendor plugin installed). So I was told that if I add in post_author to the args array and give it the ID of the vendor it will work.

 

So my code is now:

 

<?php
$artid =4;

    $args = array( 

        'post_type' => 'product', 
        'post_author' =>$artid, 
        'stock' => 1, 
        'posts_per_page' => 4, 
        'orderby' =>'date',
        'order' => 'DESC'
); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

            <li class="span3">    

                <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

                    <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>

                    <h3><?php the_title(); ?></h3>

                       <span class="price"><?php echo $product->get_price_html(); ?></span>

                </a>

                <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
            </li><!-- /span3 -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<!-- /row-fluid -->`

but this doesn't work as it just shows every product still. What Im trying to make it do is show only the last 5 products where the post_author = 4 in the DB table.

 

Can anyone see where I have gone wrong?

 

Thanks

Edited by 1clickweb

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.