Title: Orderby php help
Last modified: August 20, 2016

---

# Orderby php help

 *  [formica](https://wordpress.org/support/users/formica/)
 * (@formica)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/orderby-php-help/)
 * I’m new to php and trying to output the highest value post from star ratings 
   using:
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 * Currently it displays
 * Highest Rated Restaurant: RestaurantName1 10
 * Highest Rated Restaurant: RestaurantName2 18
 * Highest Rated Restaurant: RestaurantName3 27
 * If i could create a custom ‘orderby’ query I could also order by ‘ASC’ and limit
   posts to 1.
 * But how do I create a custom orderby query or am I going in totally the wrong
   direction here?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/orderby-php-help/#post-2498870)
 * Since I can’t see the code, I can’t be sure, but it looks like the rating field
   is a string value and you need it to be an integer.
 * Please put the code in a [pastebin](http://wordpress.pastebin.com) as suggested
   above and post a link to it here.
 *  Thread Starter [formica](https://wordpress.org/support/users/formica/)
 * (@formica)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/orderby-php-help/#post-2498888)
 * Hi vtxyzzy
 * Thanks for looking.
 * Code now in the pastebin: [http://pastebin.com/DN3TAAWL](http://pastebin.com/DN3TAAWL)
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/orderby-php-help/#post-2498914)
 * Here is a pastebin of modified code for you to try: [http://pastebin.com/4NKA6B1t](http://pastebin.com/4NKA6B1t)
 * I changed the DisplayHighestRating to GetHighestRating which returns the rating
   rather than echoing it.
 * I added a sort function rating_sort();
 * I changed the first loop to store the rating in each post as $post->rating.
 * I rewound the loop and sorted using the new function.
 * I added a second loop to display the results.
 * Since I can’t test this, it may have some bugs. If so, please post the details
   here and I will try to fix it.
 *  Thread Starter [formica](https://wordpress.org/support/users/formica/)
 * (@formica)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/orderby-php-help/#post-2498946)
 * vtxyzzy That’s brilliant – thanks so much for taking the time to help. You’ve
   no idea how much I appreciate it.
 * It works almost perfectly it outputs all ratings in a list (organised by highest
   rated first) but is there a way to limit it to display only one, the highest?
 * If I use:
 * `'posts_per_page' => 1, 'order' => 'ASC',`
 * It will limit the output but ironically display only the lowest. ‘order’ asc /
   desc seems to have no effect. I’m guessing it’s to do with the new ‘rating sort’
   function but I’m now so out of my depth it’s untrue.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/orderby-php-help/#post-2498948)
 * I think you have uncovered a flaw in the design: It only considers the number
   of posts that you have in posts_per_page, not all posts. If the highest one is
   not in that number, it will not be displayed.
 * In order to consider all posts, and not overload the server with too many queries,
   the design will need to be totally redone.
 * For the time being, set the posts_per_page to some relatively high number (say
   20 or so) and change the second loop from this:
 *     ```
       <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p class="restaurant_meta">Highest Rated Restaurant:<?php the_title(); ?><?php echo " $post->rating"; ?></p>
       <?php endwhile; ?>
       ```
   
 * to this:
 *     ```
       <?php while ($my_query->have_posts()) : $my_query->the_post();
          if (++$loop_count > 1) break; ?>
          <p class="restaurant_meta">Highest Rated Restaurant:<?php the_title(); ?><?php echo " $post->rating"; ?></p>
       <?php endwhile; ?>
       ```
   
 *  Thread Starter [formica](https://wordpress.org/support/users/formica/)
 * (@formica)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/orderby-php-help/#post-2498950)
 * vtxyzzy
 * Thank you. You’re a legend! There should in theory only be a small amount of 
   restaurants to loop through, guessing 50 max, so hopefully it’ll be ok.
 * Ultimately I’m loosing hair trying to create a restaurant review system with 
   [http://wordpress.org/extend/plugins/comment-rating-field-plugin/](http://wordpress.org/extend/plugins/comment-rating-field-plugin/)
   because I can’t get gd star rating in the new wordpress comment form without 
   paying $100 for their tutorials. Ouch!
 * Next is to work out all the above with my custom taxonomy, ie highest ‘Chinese’
   restaurant. Any pointers / tutorials appreciated.
 * Don’t even start me on the fact I’m not _correctly_ working our the highest rating,
   ie 1000 people vote 1 star to a restaurant, that’s a bad restaurant with a lot
   of stars. 10 people vote 5/5 for another and it’ll show beneath the first. Maybe
   that $100 tut is looking cheap.
 * vtxyzzy thanks again for all your help. Seriously appreciated.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/orderby-php-help/#post-2498952)
 * Perhaps your rating should be an average: If 1000 people vote 1 star, the average
   is 1, etc.
 * If you do go this way, it will be useful to say how many people the average is
   based on to give the viewer an idea of the popularity as well as the rating.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Orderby php help’ is closed to new replies.

## Tags

 * [orderby](https://wordpress.org/support/topic-tag/orderby/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [14 years, 5 months ago](https://wordpress.org/support/topic/orderby-php-help/#post-2498952)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
