How to order results by date and meta key?

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?

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

Leave a Comment