How to display the most liked post?

Hello everyone!

My app has posts and I would like to use “do a search for” to show the post with the most likes. My “posts” data has “a list of likes”. Any ideas??

Interestingly, if you go from the Posts towards Likes, it is very tricky. You can do this and I am pretty sure, it is terrible in terms of performance.

  1. Do a search for posts, sorted by creation date (or the newer ones are on top if there are equal number of likes in some posts)
  2. Advanced filter the posts by the count of likes equal to or greater than the MAX of all counts of likes:
  3. To find the MAX of all counts of likes, you have to still do a trick. In my example, I format each post as text, and combined the counts with comma, and then split them by comma and find the max:
    image

However, if your Like datatype have a column for the Post it corresponds to, it is easier.

  1. This time, you do a search for Likes, grouped by Post column and aggregated on count:
  2. Sorted by count (the result of the previous step is a grouping of likes-counts):
  3. Get the first item (the most number of likes)'s post.
1 Like

The counting method would work, although if it becomes too search-intense and consumes too much WU, here’s what I’m doing: everytime a post is ‘Liked’ it is creating a new ‘Like’, and adding +1 to the ‘total likes’ field on that thing. Now I have a stable number field to reference, display, sort by, etc. The opposite logic is true as well - when someone who liked something unlikes it, it deducts ‘1’ from that total. Now instead of needing to do a count everytime I need to reference it, I already have the total ready to go in basic number form.

1 Like

Just a note, this would become very costly workload wise at scale.

I would suggest having a data type on the post called likes which accumulates, then a second type called site stats or something. Then with a backend workflow every hour or so you can do a search for the top posts and save them as a list of top posts in your site stats object, which you use to display this.

1 Like

I think I’m missing something, somehow I get no results and the backend workflow is not working, and when it works, its updating the info as soon as click “like”:

image
image
image
image


image
image

I’ll send you a dm

Why not share here so others may see the answer?

I was offering to help you via screen share to explain the concepts etc, not sending you a DM with answers

This topic was automatically closed after 70 days. New replies are no longer allowed.