Created
March 8, 2021 10:36
-
-
Save akshuvo/5dd9d2ddb733e8c60b2b63c09de2bc51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get All Products from Last Month | |
*/ | |
add_shortcode('wc_get_products_last_month', function( $atts, $content = null ){ | |
$start_date = array( | |
'year' => date("Y", strtotime("first day of previous month")), | |
'month' => date("n", strtotime("first day of previous month")), | |
'day' => date("j", strtotime("first day of previous month")) | |
); | |
$end_date = array( | |
'year' => date("Y", strtotime("last day of previous month")), | |
'month' => date("n", strtotime("last day of previous month")), | |
'day' => date("j", strtotime("last day of previous month")) | |
); | |
$args = array( | |
'post_type' => 'product', | |
'date_query' => array( | |
array( | |
'after' => $start_date, | |
'before' => $end_date, | |
'inclusive' => true, | |
), | |
), | |
'posts_per_page' => -1, | |
); | |
$query = new WP_Query( $args ); | |
if ( $query->have_posts() ) { | |
while( $query->have_posts() ) { $query->the_post(); | |
// Get $product object from product ID | |
$product = wc_get_product( get_the_ID() ); | |
// Do you stuff here | |
//_e( $product->get_name() ); | |
} | |
} else { | |
echo 'No Posts'; | |
} | |
wp_reset_query(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment