WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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 '12 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? – Pollux Khafra Apr 28 '12 at 20:38
up vote 1 down vote accepted

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
share|improve this answer
How could I do this in the loop? How do I get the days? – Pollux Khafra Apr 28 '12 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 '12 at 21:10
The ratings score is saved as post meta as `ratings_score' – Pollux Khafra Apr 28 '12 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 );
share|improve this answer
Please include a (short) explanation/documentation on why this works and how. – tfrommen May 8 '13 at 9:15

Your Answer


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.