0

I want to store list of user IDs in the post meta who have rated a post . So, for each post I will need to check the post meta to see if the user id exists in it or not.

So my question is that, is it good idea to store that information in the post meta OR should I create a table and add it in the table. Will there be any difference in the performance (table vs meta) if the number of values grow very much?

I would prefer to store in the post meta as it is much simpler and cleaner way, but I have read one answer which says

never, ever, ever, use an EAV (aka the post_meta table) to store data that you might to need to query.

2
  • are you actually querying on the values stored in the field, or are you just loading the contents of the field and checking their values via php?
    – Milo
    Commented Nov 3, 2013 at 19:31
  • when a user clicks to vote, i get the value of that post meta and check if the user id exists in that (using php explode and foreach). If it does not exist then i add the user id in it.
    – enam
    Commented Nov 3, 2013 at 19:36

2 Answers 2

0

Based on your comment:

when a user clicks to vote, i get the value of that post meta and check if the user id exists in that (using php explode and foreach). If it does not exist then i add the user id in it.

there's no query involved there, so it's perfectly fine to use post meta for that purpose.

A query would be something like "load all posts from database where user with ID n has voted". That would be more difficult to achieve, as it's a many-to-many relationship, which WordPress doesn't do natively. Storing user IDs as serialized data (an array) is a PHP construct, MySQL doesn't "understand" that data natively, so that would not be a good use of post meta.

0

I would recommend to reconsider the custom table idea because you could check the whole "Did the user vote for a post?" in a very simple sql query like SELECT count(user_id) FROM my_votes_table WHERE post_id = 1 AND user_id = 1 which only returns a simple and small value instead of a heavy serialized array or json string that your php has to process. From a performance point of view that's really a relational database kind of task.

The integration of custom tables is really easy and I personally would say it is cleaner because post meta value has not the right data type for your kind of data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.