Get max input value custom post type wordpress

Viewed 28

I have custom post type . i want to get the maximum input value

Fo example post1 : input 10 post 2 : input 4 So , display only the maximum 10

<?php

$args = [
    'post_type' => 'cars',
    'post_status' => 'publish',
    'posts_per_page' => -1,
];

$loop = new WP_Query($args);
while ($loop->have_posts()):

    $loop->the_post();

    $price = get_post_meta(get_the_ID(), 'input', true);
    ?>
        
  <p><?php echo esc_html($price); ?></p>
  <?php
  endwhile;
?>

                            
1 Answers

You must have to set the price only after completing the while loop. Please refer to the below code:

<?php

$args = [
    'post_type' => 'cars',
    'post_status' => 'publish',
    'posts_per_page' => -1,
];

$loop = new WP_Query($args);
while ($loop->have_posts()):

    $loop->the_post();

    if(!isset($price) || get_post_meta(get_the_ID(), 'input', true) >$price ) {
        $price = get_post_meta(get_the_ID(), 'input', true);
    }
  endwhile;
?>
<p><?php echo esc_html($price); ?></p>
Related