how to get minimum price from posted custom fields of posts

This is my code (So how will i get lowest price from posts)

  <div>

it is getting subcategories from parent category

Read More
    <?php 
    $subcategories = get_categories('&child_of='.$cat.'&hide_empty');
    foreach ($subcategories as $subcategory) {
    ?>

 <div class="cat-con">
          <?php
          $subCatId=sprintf('%s',$subcategory->term_id); //the_content(); 
          query_posts('cat='.$subCatId.'&order=ASC&meta_key=price&orderby=meta_value_number');

this part of code is getting me price of custom fields

while ( have_posts() ) : the_post();

      /************************************************************************/

      $price=$post->ID; 
      $price = get_post_meta($price, 'price', true);
      if ($price <= 3500) {
      $minPrice=$price;
      /************************************************************************/
          ?>

     <div class="cat-img">
     <?php $postID=$post->ID; $image = wp_get_attachment_image_src (get_post_thumbnail_id( $pageID ),'medium', true) ; ?>
     <img width="445" height="209" src="<?php echo $image[0]; ?>" alt="" />
     </div>
                  <div class="clear"></div>
                  <div class="cat-heading">
                      <?php echo sprintf('<a href="%s">%s</a>',get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));?></a>
                  </div>
                  <div class="clear"></div>
                  <div class="cat-con-text">
                      <p><?php the_content(); ?><p>
                  </div>
                  <div class="clear"></div>

                  <div>
                      <div class="floatright">
                          <a href="<?php echo sprintf('%s',get_category_link($subcategory->term_id)); ?>"><img src="<?php bloginfo( 'template_directory' );?>/images/btn-view.png" alt="" /></a>
                      </div>
                      <div class="cat-price">Price Start From &pound;<?php echo $minPrice?></div>
                      <div class="clear"></div>
                  </div>
                      <?php    
              } // end if $price
              endwhile; ?>
        </div>
        <?php } //end foreach; ?>
        <div class="clear"></div>
    </div>
    <!--End Category-->

Related posts

Leave a Reply

2 comments

  1. Assuming you’ll echo $minPrice at the bottom (outside of the loop):

    while ( have_posts() ) : the_post();
    
    /************************************************************************/
    
    $price=$post->ID; 
    $price = get_post_meta($price, 'price', true);
    
    if ( !isset( $minPrice ) || $price < $minPrice )
        $minPrice = $price;
    
    /************************************************************************/
    
  2. `while ( have_posts() ) : the_post();
    $price1 = get_post_meta($post->ID, "price", false);
    echo $minPrice = min($price1);
    endwhile;
    `
    

    /************************/

    It is getting me value array
    Array ( [0] => 2500 )
    Array ( [0] => 3500 )