4

I am going to implement a favorite post system so users can save their favorite posts for later reading.

I want to know what is the best approach for doing it taking into considaration the possibility that hundreds of thousands of users will use this function. Users will be able to see their favorite posts in a custom page that will them.

I have thought of 3 different poissibles ways, I want to know which one would be the best performance wise.

1.- Saving all user ids as an array in a postmeta called favorites.

2.- Making a custom post type called favorites.

3.- Creating a new database table 'wp_favorites' that stores the post id, user id, and date of the created relationship.

So, which of those three would the best (as I said, performance wise)? And, what would be the pros and cons of each if any? If there is even a better way, please let me know.

1

Use a custom table. Create it as a network table, and store the site ID, so you can use just one table for the whole network.

row_id | site_id | post_id | user_id

There is no equivalent for such a table in the current core tables. Avoid serialized data, because that format is PHP specific, so you cannot read its data in any other language (including SQL), and sorting by or searching for any value is just awkward.

You have to track sites, users and posts now to delete matching entries whenever one of these objects is deleted.

0

UPDATE: I didn't see the part in your question about users being able to see a page of the favourites. Based on that and Stephens comment I think using update_user_meta() and get_user_meta() and storing post IDs would be a better option. That way it would be easier to sort and get totals with a WP_User_Query

ORIGINAL ANSWER: Save the user IDs as an array in the postmeta table. update_post_meta() and get_post_meta() will even handle serializing/unserializing the array for you.

Performance shouldn't always be your deciding factor when coming up with a solution to a programming problem. Performance is important but so is maintainable, readable and scalable code. If you go down the dedicated db table road for slight performance gains you then have to manage the creation of the table as well as all the custom data insertion and retrieval queries

Creating a new custom post type to store meta data of a post from a different post type doesn't make sense and comes with the extra overhead of a whole new post object for one piece of data on another post object.

You should be realistic when thinking about the numbers involved here, if you are positive hundreds of thousands of users are going to use this feature, all favouriting the same posts then perhaps a dedicated database table is your best bet but I find that quite unlikely which is why I've suggested using postmeta

  • If going down the post meta route you may need to do a bit of de-normalisation and store the number of favourites too. I'm assuming you'll probably want to sort posts, and you can't sorting on serialised data. Or if each user ID is a separate entry in the post meta table you could use that as the basis of the sort (but that would require some custom SQL as I don't think WordPress handles that type of sort). – Stephen Harris Jan 2 '15 at 0:21
0

The plugin User Bookmarks (discontinued now) by Pippin's Plugins did a combination: * store the "favorited" posts in user meta as serialized data * store the total favorite count in post meta as an integer

This lets you sort by favorite count which is useful. You just have to change the count when people add/remove bookmarks.

If you do go the "make your own table" route, I'd recommend using Pods as they give you a good API for working with custom tables within WordPress.

  • Does using pods involve using a plugin? I dont know much about it but I want to avoid using plugins or 3rd party code. I want to code everything myself. Why would you recommend me using pods besides the API part and could you give me some examples? Thank you. – Gixty May 2 '15 at 0:10
  • Pods is a plugin. www.pods.io - you can read on their site how it works. Why would you want to code everything yourself? I mean, you're using WP and not coding your own CMS for a reason. Well-written, tested, and supported plugins save a lot of time. – Emma Arbogast May 4 '15 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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