Show latest 30 days products in woocommerce

Hi all,
i’m trying to do a store section named newest, and showing in there the las products added in the past 30 days.

searching on internet i don’t found a good code to do this, the only that i found and work properly for the last 30 days products, also filter the complete list of products in general tab.

anybody know how can i show in “products” the complete list of products, and i the “newest” section only show the past 30 days products uploaded?

the code that i try is pasted below, and the i use the sort code [recent_products] to show it on the page


<?php
function baba_recent_products() {
    //return 'This is where the recent products should show up if I get the shortcode working. ';
	
	
	global $woocommerce_loop;

		extract( shortcode_atts( array(
			'per_page' 	=> '48',
			'columns' 	=> '2',
			'orderby' 	=> 'date',
			'order' 	=> 'desc'
		), $atts ) );

		$meta_query = WC()->query->get_meta_query();

		$args = array(
			'post_type'				=> 'product',
			'post_status'			=> 'publish',
			'ignore_sticky_posts'	=> 1,
			'posts_per_page' 		=> $per_page,
			'orderby' 				=> $orderby,
			'order' 				=> $order,
			'meta_query' 			=> $meta_query
		);

		ob_start();

		$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );

		$woocommerce_loop['columns'] = $columns;

		if ( $products->have_posts() ) : ?>

			<?php woocommerce_product_loop_start(); ?>

				<?php while ( $products->have_posts() ) : $products->the_post(); ?>

					<?php wc_get_template_part( 'content', 'product' ); ?>

				<?php endwhile; // end of the loop. ?>

			<?php woocommerce_product_loop_end(); ?>

		<?php endif;

		wp_reset_postdata();

		return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
	}
	
	
	add_shortcode( 'baba_recent_products', 'baba_recent_products' );

function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
}
add_filter('posts_where', 'filter_where');

many thanks.

Exactly what happens when you try this code? Are you getting any error messages?

no error happens, but this code filter all shotcodes i use on the site.
i need a shotcode to all products and another to show only the last 30 days newest products.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.