I'm using wp-postratings to rate posts which stores the rating as a custom meta value ratings_score. It gives you the option to use r_sortby to order the loop by highest rated but I want to give it a trending feel by dividing the $ratings_score" by how old the post is in days. So something like this..

$ratings_score = get_post_meta($post->ID,'ratings_score',true);
orderby = $ratings_score / days ago

How can I accomplish this?

  • This isn't a fully-formed-enough thought to warrant an answer, so bear with me. I'm wondering whether you could write an algorithm that takes the save date and popularity and comes up with some overall "hotness" value that you save to a new custom meta value. You could then run that formula whenever the post is saved or the popularity changes and use the standard "orderby" field to sort your loop by the meta value.
    – mrwweb
    Apr 28, 2012 at 20:17
  • I've had maybe 5 total fully formed thoughts in my life so bare with me =). What you explained is what I want to do so how would you do it exactly? Could you explain in an answer? Apr 28, 2012 at 20:38

2 Answers 2


You could do this by sorting on a calculated factor. Dividing the rating by the days gives a good starting point:

rating/days = factor (e.g. 15/6 = 2.5 or 11/3 = 3.6)

You can balance this to your needs by multiplicate the variables (rating and day) with any factor you like.

There are to ways to implement such a factor:

  1. use wp-crons to hourly refresh the factor for each post and save the value as postmeta. So you can easily use a WP_Query and sort the posts by the meta_value_num.
  2. calculate the factor directly in your post-loop
  • How could I do this in the loop? How do I get the days? Apr 28, 2012 at 20:40
  • I would look to see what the popularity plugin hooks to to save the post popularity and then hook to that with a lower priority (so the popularity is updated before you recalculate).
    – mrwweb
    Apr 28, 2012 at 21:10
  • The ratings score is saved as post meta as `ratings_score' Apr 28, 2012 at 21:37
$args = array( 'post_type' => 'product', 'numberposts' => -1, 
       'orderby' => 'meta_value_num', 'order' => get_query_var('order'), 
       'meta_key' => '_price' );
$lastposts = get_posts( $args );
  • 2
    Please include a (short) explanation/documentation on why this works and how.
    – tfrommen
    May 8, 2013 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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