admin管理员组文章数量:1416631
I am using WooCommerce and would would like to show the "out of stock" products last in the query on the archive page. How can I do that?
Currently we are making the newest product show first with the following custom query:
add_action( 'pre_get_posts', 'mik_exclude_category' );
function mik_exclude_category( $query ) {
if ( $query->is_main_query() ) {
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' );
}
}
So, we would like to do both. Show the newest products first, but show "out of stock" products last no matter how new they are.
Sincerely, Mika
I am using WooCommerce and would would like to show the "out of stock" products last in the query on the archive page. How can I do that?
Currently we are making the newest product show first with the following custom query:
add_action( 'pre_get_posts', 'mik_exclude_category' );
function mik_exclude_category( $query ) {
if ( $query->is_main_query() ) {
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' );
}
}
So, we would like to do both. Show the newest products first, but show "out of stock" products last no matter how new they are.
Sincerely, Mika
Share Improve this question asked Feb 29, 2016 at 22:38 Mika KaltoftMika Kaltoft 131 silver badge3 bronze badges1 Answer
Reset to default 3If we only have two stock statuses, namely outofstock
and instock
, we can very easily achieve sorting with pre_get_posts
add_action( 'pre_get_posts', function ( $q ) {
if ( !is_admin() // Target only front end
&& $q->is_main_query() // Only target the main query
&& $q->is_post_type_archive() // Change to suite your needs
) {
$q->set( 'meta_key', '_stock_status' );
$q->set( 'orderby', 'meta_value' );
$q->set( 'order', 'ASC' );
}
}, PHP_INT_MAX );
You should adjust that to your needs
本文标签: woocommerce offtopicShow recent products first but quotsold out lastquot in query
版权声明:本文标题:woocommerce offtopic - Show recent products first but "sold out last" in query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745256218a2650117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论