I think I need some guidance here. I have a list of custom posts on a an archive template page that displays information from each of the custom posts. They are looped through using WPQuery:

<?php $restypes = new WP_Query( array( 'post_type' => 'reservation_type', 'posts_per_page' => -1 ) );?>

Each post has a several custom fields: $price, an optional $promo_price and a variable $per which determines if the price is per-person or per-room based on a custom taxonomy. The best price is then determined like so:

  if(in_array( 'dorm', $per )){
      $person_room ='per-person';
  } else {
      $person_room ='per-room';

  $post_id= $post->ID;
  $promo_price = get_field('promo_price');
  $norm_price = get_field('price');
  $p_per_room = get_field('people_per_room');

  if ($promo_price && !empty($promo_price)){

        if($person_room == 'per-room'){
            $addprice= $promo_price /$p_per_room;
        } else {
            $addprice= $promo_price;

        $prices[$post_id]= $addprice;

  } elseif ($norm_price && !empty($norm_price)){

        if($person_room == 'per-room'){
            $addprice= $norm_price /$p_per_room;
        } else {
            $addprice= $norm_price;

        $prices[$post_id]= $addprice;

  $best = array_keys($prices, min($prices));

My problem is that the array $best is not complete until all the posts have been looped through. The point of all this is to be able to add:

if(in_array($post->ID, $best)){?> 
<div class="best-price"> 
<div class="ribbon-wrapper"><div class="ribbon">Best Price</div></div>
<?php  } else { ?>
<?php } ?>

thus giving a different style to the item with the best price per-person.

My question is would I need to create a separate loop to populate the $best array in order to do this? Or is there a better way?

I was thinking initially of using the orderby => 'meta_value_num' having calculated the best price as a function and adding a meta value.

PS The get_field() function comes from Advanced custom fields.


Well i see only two ways either you have to have seprate loop to get best value or you can add ribbon wrapper by using css and absolute positioning of div setting top and left. Though it would be best if you can calculate best value using meta_value_num.

  • So I would get and calculate all values in the first loop and use the second to display them? The ribbon is positioned fine I just need to calculate which post(s) to apply it to. If I use orderby I could simply style the first post listed but that wouldn't help if there were several with the same price per-person. – mantis Jan 22 '14 at 12:39
  • "...best if you can calculate best value using meta_value_num..."-- How? – s_ha_dum Jan 22 '14 at 14:34
  • @s_ha_dum to be honest i don't see any possibilities other than have two loops one for calculating best and than one for displaying. He can achive it using one loop with jquery by keeping best value in hidden field and then use jquery to fetch that data and display particular ribbon based on ids – Rajeev Vyas Jan 22 '14 at 14:37
  • Then why suggest calculating via meta_value_num? – s_ha_dum Jan 22 '14 at 14:39
  • @s_ha_dum "calculating via meta_value_num?" was asked in question as sugestion, so if he can calculate that would be best option. – Rajeev Vyas Jan 22 '14 at 14:52

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.