首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从子帖子自定义字段获取最低价格

如何从子帖子自定义字段获取最低价格
EN

Stack Overflow用户
提问于 2018-07-14 00:44:43
回答 1查看 22关注 0票数 0

我有一个wordpress站点,其中包含以下custom_post_type父子结构:

代码语言:javascript
复制
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) 
       ...

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

我想在产品页面级别显示一个文本,上面写着"From $lowestPrice to $highestPrice

我的问题是:

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

我知道wp_query或类似的东西可以工作吗?

我希望我说得很清楚:)

非常感谢您的帮助!

诚挚的问候,

克里斯。

EN

回答 1

Stack Overflow用户

发布于 2018-07-14 03:11:11

我用下面的代码让它工作起来:)

代码语言:javascript
复制
$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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51329611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档