0

I'm building my own simple WordPress community site and I want to include a simple like button on a custom post type. I do not want to use any plugins as I want full control and I'm also doing this for a learning experience.

I want the button to appear on each post as well as in the post archive page. If a user is logged in and likes a post, then the like button will change state to show the user that they clicked in. There will also be a page to show all of the liked posts by each user. I can handle the JS and CSS part.

My solution for implementing this feature is to create a new table in WordPress called 'wp_likes' shown below. It will log all of the users who like any custom post.

+----+----------------+---------+
| ID | Custom Post ID | User ID |
+----+----------------+---------+
|  1 |             12 |       1 |
|  2 |             13 |       1 |
+----+----------------+---------+

And a summary table wp_likes_summary. This will allow me to efficiently display the total number of likes on each custom post without writing a query to count each row.

+----+----------------+-------+
| ID | Custom Post ID | Likes |
+----+----------------+-------+
|  1 |             12 |    89 |
|  2 |             13 |    12 |
+----+----------------+-------+

Are there any dangers in using this approach? I don't want to mess with WP core tables. I believe this approach would be scaleable even if there are thousands of likes. Would it be better to store the likes in a WP core table (posts) and add a new column which contains an array of users who like the custom post? Is it even a good idea to add another column to the core posts table?

I really do appreciate your time as I'm trying to think ahead to save me hassle down the road. Thanks!

0

Using WordPress's wp_postmeta will help you a lot, instead of creating a new table. Functions like get_post_meta(), update_post_meta(), add_post_meta() etc, are readily available for use.

All you need is to create a meta_key (which in this case will be wp_likes) and the meta_value (number of likes will be stored), which can be easily retrieved using get_post_meta().

You can see the links below for references:

add_post_meta

get_post_meta

update_post_meta

delete_post_meta

  • Hi Samuel, awesome! So how can I find out which user like which post? Can the meta_key contain the custom post id and the meta_value contain the user ID? – Raja Jan 15 '17 at 20:12
  • Yes, the post ID can be stored. add_post_meta($postID, $meta_key, $meta_value). For the likes, you can use wp_likes as the meta_key, then the meta_value would store the number of likes for that post. For the user, you can use wp_likes_users as the meta_key and store the user's id as the meta_value. – Samuel Asor Jan 15 '17 at 20:39
  • Awesome, that makes sense. Thank you very much Samuel :) – Raja Jan 15 '17 at 20:40
  • Glad to help @Raja – Samuel Asor Jan 15 '17 at 20:41

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.