我有一个wordpress站点,其中包含以下custom_post_type父子结构:
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或类似的东西可以工作吗?
我希望我说得很清楚:)
非常感谢您的帮助!
诚挚的问候,
克里斯。
发布于 2018-07-14 03:11:11
我用下面的代码让它工作起来:)
$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
https://stackoverflow.com/questions/51329611
复制相似问题