Whether you’re sorting posts in your WordPress blog, products on your WooCommerce store, or topics and replies on your bbPress forum, having a system of intelligent sorting is critical to highlighting the most popular and useful content for your users.

In this post, I’m going to dive into the problems with standard sorting methods, show you the “holy grail” of sorting methods, and even share with you how you can start utilizing this weighted score sorting on your website today.

## Problems with Standard Sorting Methods

By default, WordPress likes to sort things by date, which is fine in some cases like where you want to show the most recent posts in your blog. But, in other places, you may want to surface the best content by sorting by something other than the published date.

A perfect example is a bbPress forum where you may want to show the best replies at the top of a topic page.

The standard technique for sorting such posts is sorting them by some metric like number of views, likes, or upvotes.

### Problem with Simply Sorting by Number of Votes

The problem is that the linear scale and one-dimension of these metrics can introduce cases where the post at the top with the most number of upvotes (for instance) isn’t actually the most popular or favorable result.

Even if you have 2 metrics like up votes and down votes and use them to calculate a total score consisting of up votes minus down votes, then you’re still not going to get the best sorting.

### Problem of Sorting Using Up Votes Minus Down Votes

Take for instance these two posts with their up and down votes:

- Post A: 100 up & 50 down = 50 total score
- Post B: 10 up & 1 down = 9 total score

If sorting by the total score, then Post A would be at the top because it clearly has a higher score.

But wait a second, that Post A has a lot of down votes. The ratio of up votes to down votes is 2:1. Meanwhile Post B as a much better ratio of 10:1.

### Problem of Sorting Using Ratio of Up Votes to Down Votes

So, then why don’t we just sort by ratio?

Well, let’s look at another example then…

- Post C: 50 up & 10 down = 50 total score
- Post D: 5 up & 1 down = 4 total score

Here we see both posts C & D have the same ratio of up votes to down votes (5:1). But, don’t you think Post C has more weight since it has has so many more total votes to give that ratio more authority?

## Sorting by a Weighted Score

The solution to these problems is to use a weighted score that accounts for both the ratio and the volume of votes.

Using the **Wilson binomial confidence interval calculator formula**, we are able to calculate the odds of the up votes winning vs the down votes winning after some amount of votes projected into the future.

Let’s look at the weighted scores calculated using this Wilson score interval formula.

- Post A: 100 up & 50 down = 50 total score = 0.6625 weighted score (loser)
- Post B: 10 up & 1 down = 9 total score = 0.9091 weighted score (winner)
- Post C: 50 up & 10 down = 50 total score = 0.6477 weighted score (winner)
- Post D: 5 up & 1 down = 4 total score = 0.5731 weighted score (loser)

Pretty cool, huh? With this kind of intelligent weighted scoring to use for sorting the best content to the top, you can provide so much more value to your users.

## How does the Wilson binomial confidence interval calculator formula work?

You don’t really need to know how it work in order to take advantage of it, but if you’re interested, here you go.

The Wilson binomial confidence interval calculator is a statistical method that is used to calculate a confidence interval for a binary proportion (such as the proportion of positive votes in a group of votes). The confidence interval is a range of values that is expected to contain the true proportion of positive votes with a certain level of confidence (usually 90% or 95%).

The Wilson score interval is calculated using the following formula:

`lower_bound = (positive_votes + z^2/2) / (total_votes + z^2) `

upper_bound = (positive_votes + z^2) / (total_votes + z^2)

where “positive_votes” is the number of upvotes for the topic or reply, “total_votes” is the total number of votes (upvotes and downvotes), and “z” is the z-score for the desired confidence level. The z-score is a value from the standard normal distribution that represents the number of standard deviations from the mean. For a 95% confidence level, the z-score is approximately 1.96.

The lower bound of the interval is calculated by adding the square of the z-score to the number of positive votes, and then dividing the result by the total number of votes plus the square of the z-score. The upper bound of the interval is calculated by adding the square of the z-score to both the number of positive votes and the total number of votes, and then dividing the result by the total number of votes plus the square of the z-score.

## PHP Code to Calculate the Wilson Score

```
class WilsonConfidenceIntervalCalculator {
/**
* Computed value for confidence (z)
*
* These values were computed using Ruby's Statistics2.pnormaldist function
* 1.645 = 90% confidence
* 1.959964 = 95.0% confidence
* 2.241403 = 97.5% confidence
*/
public function getScore(int $positiveVotes, int $totalVotes, float $confidence = 1.645) : float {
// Since positive can't be zero, increase both by one when there are only negative votes
if($positiveVotes == 0 && $totalVotes > 0) {
$positiveVotes++;
$totalVotes++;
}
$score = (float) $totalVotes ? $this->lowerBound($positiveVotes, $totalVotes, $confidence) : 0;
return $score;
}
private function lowerBound(int $positiveVotes, int $totalVotes, float $confidence) : float {
$phat = 1.0 * $positiveVotes / $totalVotes;
$numerator = $this->calculationNumerator($totalVotes, $confidence, $phat);
$denominator = $this->calculationDenominator($totalVotes, $confidence);
return $numerator / $denominator;
}
private function calculationDenominator(int $total, float $z) : float {
return 1 + $z * $z / $total;
}
private function calculationNumerator(int $total, float $z, float $phat) : float {
return $phat + $z * $z / (2 * $total) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $total)) / $total);
}
}
```

## How to Sort bbPress Topics and Replies by Weighted Score

If you have a bbPress forum on your WordPress site and you want to add a user voting system and utilize a weighted score for sorting the topics and/or replies, you’re in luck!…

I developed the **bbPress Voting plugin** to add a voting system to your bbPress forum for free, and I developed the **bbPress Voting Pro plugin** to give you several advanced features including sorting by weighted voting score.

These plugins are a must-have for any bbPress forum. I really hope you enjoy them!

Blogger, expert WordPress developer, and developer of the awesome bbPress Voting plugin which is a must-have plugin for any bbPress forum.

Download bbPress Voting for free on the WordPress Plugin Directory.