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.
My Answer:
php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'date_query' => array(
array(
'after' => '10 days ago'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
Rate this post
3 of 5 based on 8308 votesComments