1

I am using Advanced Custom Fields to get a list of properties as follows:-

        <?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(); ?>

The following; <?php the_field('price'); ?>‘ returns:-

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

What I need is to get the lowest and highest value as a variable, any ideas how I would do this?

  • is each price field a single value, or multiple values separated by spaces? – Milo Oct 21 '15 at 22:03
  • multiple values separated by spaces – nsilva Oct 22 '15 at 8:14
0

Without knowing whether returns a single value, an array, or a string of values separated by spaces, I'd suggest something like:

<?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(); ?> 
0

Got it working in the end with the below code:

    <?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(); 
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.