• I’m new to php and trying to output the highest value post from star ratings using:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    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)
  • 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 as suggested above and post a link to it here.

    Thread Starter formica

    (@formica)

    Hi vtxyzzy

    Thanks for looking.

    Code now in the pastebin: https://pastebin.com/DN3TAAWL

    Here is a pastebin of modified code for you to try: https://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

    (@formica)

    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.

    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

    (@formica)

    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 https://www.ads-software.com/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.

    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.