Last active
October 26, 2023 16:07
-
-
Save luizbills/575ad3fe8a35314d2762b5c32a515cfc to your computer and use it in GitHub Desktop.
Programmatically populate the “Sale” product category with products on sale
This file contains 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 | |
/** | |
* Based on https://www.businessbloomer.com/woocommerce-sale-category-automatic/ | |
*/ | |
add_action( 'pre_get_posts', 'luizpb_wc_sale_category' ); | |
function luizpb_wc_sale_category ( $query ) { | |
$term_slug = 'sale'; | |
if ( $query->get( 'product_cat' ) !== $term_slug ) return; | |
$query->set( 'product_cat', null ); | |
$query->set( 'post_type', 'product' ); | |
$query->set( 'post__in', wc_get_product_ids_on_sale() ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment