Skip to content

Instantly share code, notes, and snippets.

@luizbills
Last active October 26, 2023 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizbills/575ad3fe8a35314d2762b5c32a515cfc to your computer and use it in GitHub Desktop.
Save luizbills/575ad3fe8a35314d2762b5c32a515cfc to your computer and use it in GitHub Desktop.
Programmatically populate the “Sale” product category with products on sale
<?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