【问题标题】:How to get lowest price from custom fields of child posts如何从子帖子的自定义字段中获得最低价格
【发布时间】:2018-07-13 16:44:42
【问题描述】:

我有一个带有以下 custom_post_type 父子结构的 wordpress 网站:

Brand           (main parent post)
    Product     (child post of Brand, but parent post for versions) 
       Version  (child post of Product) 
       Version  (child post of Product) 
       Version  (child post of Product) 
       ...

每个版本的子帖子都有一个$price post_meta 字段,具有不同的值。

我想在产品页面级别显示“From $lowestPrice to $highestPrice”的文字

我的问题是:

如何在所有版本子帖子 post_meta 字段中检索 $price 的最低和最高值,以便构建 $lowestPrice$highestPrice 的值

我知道 wp_query 或类似的东西会起作用?

我希望我很清楚:)

非常感谢您的帮助!

最好的问候,

克里斯。

【问题讨论】:

  • 是的,WP_Query 是我在这种情况下会采用的方式。查看文档,如果您遇到问题,请更新您的问题(最好也包含一些代码),我们会为您提供帮助。

标签: wordpress


【解决方案1】:

我得到了它的工作:)

$parent_id = $posts[0]->ID;
$args = array(
    'post_parent' => $parent_id,
    'post_type' => 'fichas',
    'post_status' => 'publish',
    'posts_per_page' => - 1,
    'ignore_sticky_posts' => 1
);
$properties_query = new WP_Query($args);
$prices = array();

if ($properties_query->have_posts()):
    while ($properties_query->have_posts()):
        $properties_query->the_post();
        $price = get_post_meta($post->ID, 'precio_oferta', true);
        if (isset($price) && !empty($price))
            {
            $prices[] = $price;
            }

    endwhile;
    $max_price = max($prices);
    $min_price = min($prices);
endif;

wp_reset_query();

echo $max_price; // displays the max price
echo $min_price; // displays the minumum price

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多