woocommerce_products_will_display() 是可插入的,这意味着您可以在自己的 functions.php(或特定于站点的插件)中定义它并对其进行更改,让它为您的特定模板/页面返回 true。
我认为它可以代表一些调整和过滤器。
编辑
我玩了这个多一点。通常更改您要检索的帖子是在 pre_get_posts 挂钩中完成的。我看了一下 WooCommerce 在做什么。他们正在向pre_get_posts 钩子添加一些内容,并从那里调用他们的特殊查询内容。
但是,如果您不在 WooCommerce 页面上,他们的特殊查询内容就会消失。所以,这让我觉得也许我们可以从我们自己的函数中自己调用它。我把它放在一起,再加上一个名为“on-sale”的页面的特殊页面模板(基本上只是商店模板的副本),似乎只显示了具有适当排序和分页的待售商品。
您的里程可能会有所不同,但我希望它会有所帮助。
function kia_pre_get_posts( $q ){
// We only want to affect the main query
if ( ! $q->is_main_query() ) {
return;
}
// Fix for verbose page rules
if ( is_page('on-sale') ) {
$q->set( 'post_type', 'product' );
$q->set( 'page_id', '' );
$q->set( 'page', '' );
$q->set( 'pagename', '' );
$meta_query = array( array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>'
) );
$q->set( 'meta_query', $meta_query );
if ( isset( $q->query['paged'] ) ) {
$q->set( 'paged', $q->query['paged'] );
}
// Fix conditional Functions
$q->is_archive = true;
$q->is_post_type_archive = true;
$q->is_singular = false;
$q->is_page = false;
}
$wc_query = WC()->query;
$wc_query->product_query( $q );
if ( is_search() ) {
add_filter( 'posts_where', array( $wc_query, 'search_post_excerpt' ) );
add_filter( 'wp', array( $wc_query, 'remove_posts_where' ) );
}
add_filter( 'posts_where', array( $wc_query, 'exclude_protected_products' ) );
// We're on a shop page so queue the woocommerce_get_products_in_view function
add_action( 'wp', array( $wc_query, 'get_products_in_view' ), 2);
// And remove the pre_get_posts hook
$wc_query->remove_product_query();
}
add_action( 'pre_get_posts', 'kia_pre_get_posts' );