Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Here is my Code.

It is get sub category from main category. and from sub category having different posts related about different products. Now issue it this that i want lowest price value from custom field that i define. so i should i do it to get lowest/min value.

<div>
    <?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');
          while ( have_posts() ) : the_post();

          /************************************************************************/
          // this part of code is getting me price of custom fields   
          $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-->
share|improve this question
    
Please can you format the source, that it easier to read. Current it is hard to understand your template code. Also it is fine, if you have more descriptions to the source. –  bueltge Jan 29 '13 at 10:44
    
now i have updated –  user26461 Jan 29 '13 at 10:48
    
Now I have updated and I hope you see it, it is better. The source is from one template file and the important topic for you is marked with comments inside this source. –  bueltge Jan 29 '13 at 10:52
    
Also a hint; don't use $post->ID; use get_the_ID(). You set very often the var new with same value ID, not useful etc. –  bueltge Jan 29 '13 at 10:57
add comment

1 Answer

up vote 0 down vote accepted

Your code is badly broken in a couple of place and potentially flawed in a couple of others.

$price=$post->ID; 
$price = get_post_meta($price, 'price', true);
i0 ($price <= 3500) {
   $minPrice=$price;
  • Line 1: Not broken but why not just use $post->ID? Setting that to a variable named $price is confusing and unnecessary.
  • Line 2: Passing the true parameter means get_post_meta will only return a single value. This will break if there are multiple prices. I don't know if that is the case.
  • Line 3: if is misspelled. This would be a fatal error. Secondly, your question is about finding the lowest value but you are looking for less than or equal to a static 3500. There is an inconsistency. I am assuming you want the lowest value.
  • Finally: You don't close the if conditions. There should be a closing }. This is another fatal error.

So, if you are trying to get the lowest value per post do this:

$price = get_post_meta($post->ID, 'price');
$minPrice=min($price);

If you are trying to get the lowest price from all of the posts in your loop do this:

$price = get_post_meta($price, 'price', true);
// initialize $minPrice to some high value before the Loop starts
if ($price < $minPrice) {
    $minPrice=$price;
} 
share|improve this answer
1  
The if ($price was a problem of my edit, Sorry. But the code was not readable and I have change small on to see the source in one code block on the question. –  bueltge Jan 29 '13 at 16:28
    
i am get sub-categaries and there all posts which belong to sub-categaries. now i want to display that post which has lowest price –  user26461 Jan 30 '13 at 6:25
    
while ( have_posts() ) : the_post(); $price1 = get_post_meta($post->ID, "price", false); echo $minPrice = min($price1); endwhile; –  user26461 Jan 30 '13 at 7:10
    
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 ) –  user26461 Jan 30 '13 at 7:59
    
Is your $price value an array (serialized array) in the database? I tested this and I do not get an array. –  s_ha_dum Jan 30 '13 at 14:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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