0

I was wondering if i should create a separate table for likes counter and storing post id and user id for likes.

Ex table: ID, post_id, User_id

or should if put it in users table to keep database clean and small like this:

Ex already existing users_meta table(Wordpress)

User_id, post_likes(meta_value), post_ids(array(2,3,4,5,6))

So whenever user likes a post, just push the post id to specific user's table.


The idea of putting id's in user meta table is. It will keep the database system small and easy for finding if user has liked any post.

Please correct me if i'm wrong!

Daman
  • 473
  • 2
  • 7
  • 17

1 Answers1

0

I'd say the best argument for storing the post id's in user meta is that you don't have to rewrite a bunch of functions from scratch to interact with a brand new table in the database. You can use the prebuilt WordPress functions like update_user_meta(), etc to do the reading / writing.

I've seen this exact use done by storing all the posts a user 'liked' as a serialized array in a user meta field via the maybe_serialize function:

https://codex.wordpress.org/Function_Reference/maybe_serialize

You create an array of post ids, run it through maybe_serialize(), and then save it as a value to the user's meta table. Pretty clean and simple.

Greg Burkett
  • 1,905
  • 1
  • 12
  • 14