• Resolved shenalorlof

    (@shenalorlof)


    Hi dear supporters,

    i want to Display latest 6 products in a row in a random order from the latest 20 product posts.

    My points should be in the algorithm is…..
    1. display 6 products in row, 2. from last 20 latest products, 3. in a random order. All these should be in same

    My earlier topic i started a supporter asked me to use “[products limit=”6″ orderby=”rand”]” this shortcode doesnt do what i need.. BUT i clearly want the latest product in a random order FORM THE LAST 20 PRODUCTS.

    i want it from the last 20 latest products..

    Thank You in advance ?? <3

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hey there,

    The tricky part here is getting the 20 latest products and then showing only six of them. The shortcode’s limit option is there to set how many are loaded per page. There’s not a set of options to take 20 items and then show 6 random ones.

    I think this query should get you to where you want to go. You could use it in a theme template or turn it into a shortcode.

    
    $args = array(
    	'post_type' => 'product',
    	'posts_per_page' => 20,
    	'orderby' => 'date',
    	'no_found_rows' => true,
    	);
            $counter = 1;
            $max = 6;
    	$loop = new WP_Query( $args );
    	shuffle($loop->posts);
    	if ( $loop->have_posts() ) {
    			while ( ($loop->have_posts()) && ($counter <= $max) ) : $loop->the_post();
    				wc_get_template_part( 'content', 'product' );
                    $counter++;
    			endwhile;
    		} else {
    			echo __( 'No products found' );
    		}
    		wp_reset_postdata();
    

    Essentially this is a query for the 20 newest products. We then take that list and randomize it with shuffle(). Then we add a counter to the loop so that it only shows results one through six.

    I hope that gets you pointed to a solution.

    Thread Starter shenalorlof

    (@shenalorlof)

    @3sonsdevelopment
    Hi wow, thank u for up response supporter. finally. This is great. Can u do me one more favour. Can I get this code in full please. With the shuffle and function so I can add to a shortcode. Thank you in advance.

    Hey @shenalorlof,

    See if this works for you. It’s provided “as is” but hopefully it’ll get you pointed in the right direction. This uses a shortcode called [randomprod]. It doesn’t take any attributes.

    
    function ijab_random_products() {
    	?>
    
    	<ul class="products columns-3">
    		<?php
    		$args = array(
    			'post_type' => 'product',
    			'posts_per_page' => 20,
    			'orderby' => 'date',
    			'no_found_rows' => true,
    		);
    		$counter = 1;
    		$max = 6;
    		$loop = new WP_Query( $args );
    		shuffle($loop->posts);
    		if ( $loop->have_posts() ) {
    			while ( ($loop->have_posts()) && ($counter <= $max) ) : $loop->the_post();
    				wc_get_template_part( 'content', 'product' );
    				$counter++;
    			endwhile;
    		} else {
    			echo __( 'No products found' );
    		}
    		wp_reset_postdata();
    		?>
    	</ul>
    
    	<?php 
    }
    add_shortcode( 'randomprod', 'ijab_random_products' );
    
    Thread Starter shenalorlof

    (@shenalorlof)

    Hi @3sonsdevelopment. First I would like to thank you great support.. there’s a problem , unfortunately not working ?? . it shows me old products as well which i uploaded last year.

    Can we do like this, the logic..
    First we sort products by the date,
    Second take 20 products starting from the first row
    Third shuffle that 20 products we got and display 6 in a row

    Can we do this and get this done @3sonsdevelopment

    Thank you

    Thread Starter shenalorlof

    (@shenalorlof)

    Can you change this to as in the above please i want to display 6 products from the latest 20 products.. like this https://ibb.co/3cxn43C @3sonsdevelopment
    Hope this code might give an idea..

    $recent_posts = wp_get_recent_posts( array(
    ‘numberposts’ => 20, // Number of recent posts
    ‘post_status’ => ‘publish’, // Show only the published posts
    ‘post_type’ => ‘product’
    ));

    $sub = array_splice( $recent_posts, 10, 10 );// array_splice ( array, offset, length )

    shuffle( $sub ); // Random

    array_splice( $recent_posts, 10, 0, $sub );

    foreach( $recent_posts as $post ) {
    echo $post[‘ID’] . ‘<br>’;
    //echo ‘

    ', print_r( $post, 1), '

    ‘;
    } // Loop

    wp_reset_query();

    • This reply was modified 3 years, 7 months ago by shenalorlof.

    Hey @shenalorlof,

    I gave the following code a test on my development site. I refreshed the page a dozen times or so and it never returned any products outside of the 20 most recent. I set the product list in the admin to show 20 products per page and every product it displayed was one of those. It’s just a slight change to the wp_query.

    
    function ijab_random_products() {
    	?>
    
    <ul class="products columns-3">
    	<?php
    $args = array(
    	'post_type'		=> 'product',
    	'posts_per_page'	=> 20,
    	'orderby'		=> 'date',
    	'order'			=> 'DESC',
    	'no_found_rows'		=> true,
    	);
            $counter = 1;
            $max = 6;
    	$loop = new WP_Query( $args );
    	shuffle($loop->posts);
    	if ( $loop->have_posts() ) {
    			while ( ($loop->have_posts()) && ($counter <= $max) ) : $loop->the_post();
    				wc_get_template_part( 'content', 'product' );
                    $counter++;
    			endwhile;
    		} else {
    			echo __( 'No products found' );
    		}
    		wp_reset_postdata();
    	?>
    </ul>
    
    	<?php
    }
    add_shortcode( 'randomprod', 'ijab_random_products' );
    
    Thread Starter shenalorlof

    (@shenalorlof)

    Hello @3sonsdevelopment, hope you doing good.
    I uploaded like 100 products in this year but it shows product I added n 2020 December as well. ?? any conflict? I’m uploading products through WCFM multivendor cz this is a multivendor site

    Thanks for trying. You may need to reach out to a developer who can log into the site and get this working for you. There are freelancers available at Codeable who could do that for you.

    https://woocommerce.com/codeable/

    Cheers

    Thread Starter shenalorlof

    (@shenalorlof)

    Thank you @3sonsdevelopment . Xd. Thank you for your help. Thumbs up. ?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Display 6 per row products in a random order from the latest 20 product posts’ is closed to new replies.