新的和喜欢的内容的后期排序算法(或wordpress插件)

I want to make a website which will be having posts (users can like or dislike the posts). I want to sort the posts combined with their freshness and as liked by users.But below is the problem.

  1. If I displays posts by post date newer post will always display on top then users will not get old posts (which may be liked by many persons).
  2. If I sorts the posts by most liked posts the Fresh posts will move downwards as new posts have not got enough time to be liked by users.

So I want an algorithm (or a wordpress plugin as I am using wordpress) so that I can display the combination of new and most liked contents on my website.

You don't want an algorithm but you want to directives to design your own algorithm.

Here is what you are going to do:

  • Collect like the 30 past days posts
  • Valuate the date score (10 last days = 5 points, 10 - 20 = 3 points, 30 days = 1 point, etc...)
  • Valuate the freshness score (500+ likes = 10 points, 200 - 500 likes = 5 points, etc...)
  • Create a function 'getScore(post)' which will give you the score of a post taking in account the freshness and date criteria. (You can add more criterias if you want to be more accurate)
  • Create/Sort an array in order to have the posts with the biggest scores first.

This kind of algorithms can have many use-cases (recommendation of a video using its matching score with a user according to the video's attributes, the user's history, similar users feedback on the video, etc...) and designing by yourself will teach you a lot.

Good luck.

Edit: That's more the pure PHP way than wordpress, thanks to Paul for posting the wordpress way.

Try to define more specific rules, for how the posts are sorted. In your case, you want to sort descending by the number of posts and ascending by the age of the post.

define compare:
    input: date ageA , date ageB , int likesA , int likes B
    output: int

    double ageFactor
    double likesFactor

    long ageA = diffSeconds(today() , ageA)
    long ageB = diffSeconds(today() , ageB)

    return (int) ((likesA - likesB) * likesFactor + (ageA - ageB) * ageFactor)

Basically all you'd have to do is to find the right values for ageFactor and likesFactor to balance the influence of likes and age of a post on it's position. The resulting int would be positive, if post A (ageA and likesA) should be further up than post B.