questions

Showing products only added in the last 10-days woocommerce

Rate this post

Im trying to make a woo commerce page that only shows the latest products from the last 10 days. This code below works.

<?php get_template_part('partials/page-title'); ?>
<div class="row padded full-page" style="margin-top: 50px;">
<ul class="products columns-4">
    <?php
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 100,
            'order' => 'DESC',
            'date_query' => array(
                'after' => date('Y-m-d', strtotime('-10 days')) 
            )
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
            
                wc_get_template_part( 'content', 'product' );
            endwhile;
            
        } else {
            echo __( 'No products found' );
        }
        

        wp_reset_postdata();
    ?>
</ul>

</div>
<?php get_footer(); ?>

However if there are more than 100 products added in the last 10 days. (unlikely) there is no pagination available. This I’d imagine is down to the fact its on a page? file name: "page-new-arrivals.php"

I have tried to add

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
        'post_type' => 'product',
        'posts_per_page' => 100,
        'order' => 'DESC',
        'paged'=>$paged,
        'date_query' => array(
            'after' => date('Y-m-d', strtotime('-10 days')) 
        )
        );

To no avail. Also tried this.

$GLOBALS['wp_query']->max_num_pages = $loop->max_num_pages;
                the_posts_pagination( array(
                   'mid_size' => 1,
                   'prev_text' => __( 'Back', 'green' ),
                   'next_text' => __( 'Onward', 'green' ),
                   'screen_reader_text' => __( 'Posts navigation' )
                ) );

I have no idea! I’m a bit lost. What do I need exactly? Is there a way to inject into the woocommerce loop via functions, to limit products shown so they are no older than 30 days. Any help would be appreciated.

 

✔️Solution:

So I decided to look at this from another angle and hook into the shortcode something that is already provided and i know the pagination works.

add_filter('shortcode_atts_products', 'htdat_shortcode_atts_products', 10, 4);
function htdat_shortcode_atts_products( $out, $pairs, $atts, $shortcode ){
if ( isset ($atts[ 'daysold' ]) && $atts [ 'daysold' ] ) {
$out[ 'daysold' ] = true;
} else {
$out[ 'daysold' ] = false;
}
return $out;
}

add_filter( 'woocommerce_shortcode_products_query', 'htdat_woocommerce_shortcode_products_query', 10, 2 );
function htdat_woocommerce_shortcode_products_query( $query_args, $attributes ) {

if ( $attributes[ 'daysold' ] ) {
$query_args[ 'date_query' ] = array ('after' => date('Y-m-d', strtotime('-30 days')));
}
return $query_args;
}

[products daysold="true" limit="12" columns="4" paginate="true" orderby="date"]

Above code works perfectly!

Leave a Reply

Your email address will not be published.

Back to top button