Created
November 17, 2021 09:36
-
-
Save gzalinski/3a3547a75b1a5873c6ba686f910fca56 to your computer and use it in GitHub Desktop.
Woocommerce product query on sale between two date
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 | |
$query_args = array( | |
'post_type' => [ 'product_variation', 'product' ], | |
'posts_per_page' => - 1, | |
'meta_query' => array( | |
'relation' => 'AND', | |
array( | |
'key' => '_sale_price_dates_from', | |
'value' => time(), | |
'compare' => '<=' | |
), | |
array( | |
'key' => '_sale_price_dates_to', | |
'value' => time(), | |
'compare' => '>=' | |
) | |
), | |
'meta_key' => '_sale_price_dates_to', | |
'order' => 'ASC', | |
'orderby' => 'meta_value_num', | |
); | |
$promo_product_ids = []; | |
$post_ids = get_posts( $query_args ); | |
foreach ( $post_ids as $post ) { | |
if ( $post->post_type == 'product' ) { | |
$promo_product_ids[] = $post->ID; | |
} elseif ( 'product_variation' ) { | |
if ( ! in_array( $post->post_parent, $promo_product_ids ) ) { | |
$promo_product_ids[] = $post->post_parent; | |
} | |
} | |
} | |
foreach ( $promo_product_ids as $product_id ) : | |
$post_object = get_post( $product_id ); | |
setup_postdata( $GLOBALS['post'] =& $post_object ); | |
wc_get_template_part( 'content', 'product' ); | |
endforeach; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment