• 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

    i want it from the last 20 latest products..

    I got following code as a solution from a supporter but it shows very old products as well

    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' );

    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

    • This topic was modified 3 years, 7 months ago by shenalorlof.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter shenalorlof

    (@shenalorlof)

    Dear supporters,

    Can we change the above code something like this code please? i want to display 6 products from the latest 20 products in a random order (shuffle).. like this https://ibb.co/3cxn43C

    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();

    >> Show 6 products in a row from the last 20 products in a random order << i want it to be show like this as in the image https://ibb.co/3cxn43C

    Thank you in advance dear supporters

    • This reply was modified 3 years, 7 months ago by shenalorlof.
    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there ??

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Since it’s been a while since we last heard back from you, I’m going to mark this thread resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Cheers! ??

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