Hi. I have a rating field in my entries, how can I get average rating score from all entries?
I never used the rating field before, so I gave it a test. The collection name in my example is ratingtest
and the field name is rating
. I have three entries with rating: 1, 5, 5.
You can create a custom api endpoint /config/api/average.php
:
<?php
$entries = cockpit('collections')->find('ratingtest');
// add your checks here to prevent division by zero or to filter entries without a rating...
$ratings = array_column($entries, 'rating');
return ['averageRating' => array_sum($ratings) / count($ratings)];
Now call your api https://example.com/api/average?token=xxtokenxx
.
Output: {"averageRating":3.6666666666666665}
Because Wordpress is for one-click-solutions without caring about the consequences, e. g. privacy (GDPR compliance) or bloated plugins. And because it is a pain in the ass to modify Wordpress. It’s source code is very hard to read, there are tons of functions in the global scope with nearly similar names and you have to install around 10 addons to disable privacy and security related bad design decisions before you can even start to use it.
Working with Cockpit is the other way around. You don’t disable half of the core features. Instead you extend it with a handful of features, that you really need.