大多数必要的信息可以在Code Reference: WP_Query
| Class . 其他有用资源Theme Handbook: Conditional Tags. 下面的代码包含一些明确的注释。请注意,这是未经测试的代码,因此不能保证,但它应该可以让您继续。
add_action( \'woocommerce_product_query\', \'custom_woocommerce_product_query\' );
function custom_woocommerce_product_query( $q ) {
if ( ! is_admin() ) {
//query for the out of stock items we want to exclude
$oos_query = new WP_Query( [
// items must be older than 1 month
\'date_query\' => [ [
\'column\' => \'post_date\',
\'before\' => \'1 month ago\'
], ],
//only out of stock items...
\'meta_query\' => [ [
\'key\' => \'_stock_status\',
\'value\' => \'outofstock\',
\'compare\' => \'=\',
], ],
//we want products, nothing else
\'post_type\' => \'product\',
//we want them all
\'posts_per_page\' => -1,
//the ids are enough, no need to get more data
\'fields\' => \'ids\',
] );
//getting array of ids from object
$exclude_ids = $oos_query->posts;
//excluding the above queried products from woocommerce\'s product query
$q->set( \'post__not_in\', $exclude_ids );
}
}