【问题标题】:Order by stock by new WP_Query - Woocommerce - Wordpress按新 WP_Query 按库存订购 - Woocommerce - Wordpress
【发布时间】:2023-01-14 05:58:15
【问题描述】:

我正在制作一个新的 WP_Query 来搜索产品。

这向我显示了 wp_post 信息,但没有显示股票信息。

如何在搜索中将库存结果排在最前面,将非库存结果排在最后。

这是我当前的代码。

$args = array(
'post_type' => 'product',
'post_status'           => 'publish',
'ignore_sticky_posts'   => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'posts_per_page'=> $limit
);

$list = new WP_Query( $args );

试试这个代码,但它不起作用

$args = array(
'post_type' => 'product',
'post_status'           => 'publish',
'ignore_sticky_posts'   => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => '_stock',
'posts_per_page'=> $limit
);

$list = new WP_Query( $args );

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    第二个 sn-p 看起来不错请确保您的 _stock 字段是一个数值,以便 meta_value_num orderby 类型可以正常工作。

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 2021-01-21
      • 2021-11-03
      相关资源
      最近更新 更多