Skip to content

Instantly share code, notes, and snippets.

@braddalton
Last active May 17, 2024 11:48
Show Gist options
  • Save braddalton/8716621e7a26926015ce76cd5e24cf85 to your computer and use it in GitHub Desktop.
Save braddalton/8716621e7a26926015ce76cd5e24cf85 to your computer and use it in GitHub Desktop.
Custom loop in WooCommerce that displays specific products between specific dates. Full Code https://wpsites.net/?p=115983
function display_custom_products_before_category() {
// Define the product IDs you want to display
$custom_product_ids = array(123, 456, 789); // Replace with your product IDs
// Set the date range to display the custom products (for 1 month)
$start_date = '2024-05-01'; // Start date in 'Y-m-d' format
$end_date = '2024-06-01'; // End date in 'Y-m-d' format
$current_date = date('Y-m-d');
// Check if current date is within the range
if ($current_date >= $start_date && $current_date < $end_date) {
$args = array(
'post_type' => 'product',
'post__in' => $custom_product_ids,
'orderby' => 'post__in'
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) {
echo '<div class="custom-products">';
while ($custom_query->have_posts()) {
$custom_query->the_post();
wc_get_template_part('content', 'product');
}
echo '</div>';
wp_reset_postdata();
}
}
}
add_action('woocommerce_before_main_content', 'display_custom_products_before_category', 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment