admin管理员组文章数量:1310819
I am sorting product on product category page. I want to display sale product at first and show remaining product after it. I am using pre_get_posts hook to modify query.
My CODE
function modify_query( $query ) {
if( ! is_admin() && $query->is_main_query() && is_product_category() ){
$product_ids_on_sale = wc_get_product_ids_on_sale();
$query->set( 'post__in', $product_ids_on_sale );
$query->set('orderby', 'post__in' );
}
}
add_action( 'pre_get_posts', 'modify_query' );
But this query is only showing sale products. I want to show ramaning post after the sale products. For example if my post_per_page is 10, and sale product is 2. Then I want to display 2 sale and 8 remaining products.
I am sorting product on product category page. I want to display sale product at first and show remaining product after it. I am using pre_get_posts hook to modify query.
My CODE
function modify_query( $query ) {
if( ! is_admin() && $query->is_main_query() && is_product_category() ){
$product_ids_on_sale = wc_get_product_ids_on_sale();
$query->set( 'post__in', $product_ids_on_sale );
$query->set('orderby', 'post__in' );
}
}
add_action( 'pre_get_posts', 'modify_query' );
But this query is only showing sale products. I want to show ramaning post after the sale products. For example if my post_per_page is 10, and sale product is 2. Then I want to display 2 sale and 8 remaining products.
Share Improve this question asked Jan 6, 2021 at 4:38 PrajwolPrajwol 11 bronze badge1 Answer
Reset to default 0I think the simplest, but not necessarily the most performant, would be using two WP_Query loops, one using $query->set( 'post__in', $product_ids_on_sale );
and other using $query->set( 'post__not__in', $product_ids_on_sale );
because these are mutually exclusive. One loop would be used for the main query and other for second loop as show in this example in the documentation.
You would need to account for the number of items in each loop based on the number of items in the $product_ids_on_sale
variable AND pagination. And I would the main query for the products NOT in sale.
本文标签: sortHow to display remaining post ( in postin ) if posts are less then post per page
版权声明:本文标题:sort - How to display remaining post ( in post__in ) if posts are less then post per page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741860490a2401594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论