ACF-从字段获取最低和最高值

时间:2015-10-21 作者:nsilva

我使用高级自定义字段获取属性列表,如下所示:-

        <?php
            $args = array(
                \'posts_per_page\'=> -1,
                \'post_type\'     => \'properties\',
                \'meta_key\'      => \'development\',
                \'meta_value\'    => $development_id
            );
            $the_query = new WP_Query( $args ); 

        ?>
        <?php if( $the_query->have_posts() ): ?>
            <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <div class="col-md-4">
                <?php the_title(); ?>
                <?php the_field(\'price\'); ?>
                Plot <?php the_field(\'plot_no\'); ?>
                </div>
            <?php endwhile; ?>
        <?php endif; wp_reset_query(); ?>
以下内容:;<?php the_field(\'price\'); ?>‘ 返回值:-

325000 489950 329000 325000 294995 199950 294995 252950 325000 257950 197950 325000

我需要的是得到变量的最低值和最高值,你知道我该怎么做吗?

2 个回复
最合适的回答,由SO网友:nsilva 整理而成

使用下面的代码最终实现了:

    <?php
        $args = array(
            \'posts_per_page\'=> -1,
            \'post_type\'     => \'properties\',
            \'meta_key\'      => \'development\',
            \'meta_value\'    => $development_id,
        );
        $properties_query = new WP_Query( $args ); 
        $prices = array();

        if( $properties_query->have_posts() ):
            while( $properties_query->have_posts() ) : $properties_query->the_post();
                $price = get_field(\'price\'); 
                if(isset($price) && !empty($price)){
                    $prices[] = $price; 
                }
            endwhile;
            $max_price = max($prices);
            $min_price = min($prices);

        endif; wp_reset_query(); 
    ?>

SO网友:darrinb

如果不知道是返回单个值、数组还是由空格分隔的值字符串,我建议如下:

<?php
$args = array(
    \'posts_per_page\'=> -1,
    \'post_type\'     => \'properties\',
    \'meta_key\'      => \'development\',
    \'meta_value\'    => $development_id
);
$the_query = new WP_Query( $args ); 

?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <?php $min_price = $max_price = \'\'; ?>
    <div class="col-md-4">
    <?php the_title(); ?>
    <?php 
        $price = get_the_field(\'price\'); 
        if( $price ) {
            // if it\'s an array already:
            $min_price = min($price);
            $max_price = max($price);

            // if it\'s not an array already:
            $prices = explode(\' \', $price);
            $min_price = min($prices);
            $max_price = max($prices);            
        }
        ?>
    <?php the_field(\'price\'); ?>
    Plot <?php the_field(\'plot_no\'); ?>
    </div>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?> 

相关推荐

Increase offset while looping

我正在编写一个自定义帖子插件,它将自定义帖子分组显示为选项卡。每组4个岗位。是否可以编写一个偏移量随每次循环而增加的查询?因此,结果将是:-第一个查询显示从1到4的帖子-第二个查询显示从5到8的帖子-第三个查询显示从9到12的帖子等。 <div class=\"official-matters-tabs\"> <?php $args = array(\'post_type\' => \'official-matters\', \'showp