1

I am pretty new to WordPress API, loving it btw, I have a small project, adding a like button to the end of every post, pressing it makes user like the post, button changes to dislike, pressing again makes user dislike the post.

Without the knowledge of WordPress API, I planned to create a table for my plugin in wpdb which I will store the post-id and user-ids. Plugin will know by querying the table if the user has already liked the post. But on the internet I found a lot of examples of like buttons, not creating a custom table and using update_post_meta, and if I understood right update_post_meta does not alter any table in the database or insert new rows. Because I tried some of the plugins which uses update_post_meta and after liking a post, my wp_posts and wp_postmeta tables does not change at all.

My question is where exactly is post_meta stored, where does wordpress store the new custom field 'like_count' if I do this;

update_post_meta($post_id, 'like_count',1);
1
  • See wp_postmeta table in your database, where wp part of the table name is prefix defined in wp-config.php.
    – Max Yudin
    Mar 31, 2016 at 9:12

2 Answers 2

1

Yes, we can use existing tables in WordPress for storing the values.
Post meta fields are stored in the {$wpdb->prefix}_postmeta table (depending on the table prefix; wp_postmeta by default). If the meta key "like_count" is already present in the table along with the post ID then update_post_meta() will update this, and otherwise this function will insert a new row with this key.

For more reference you can check here : https://codex.wordpress.org/Function_Reference/update_post_meta

3
  • I was confused because, totally my fault, I was not looking all the rows of the postmeta table, after clicking show all, I saw what I was expecting and what you said. But there are lots of unintended junk data left after uninstalling plugins I don't use anymore. Isn't it better to just create custom table and then drop the table if the plugin uninstalled to prevent these junks? Mar 31, 2016 at 11:29
  • @EralpŞahin Please tell me how many data you want to add in to the post_meta table. 1. If you are adding only "like_count" along with post_id then when you will uninstall the plugin you can remove all the rows which contain "like_count" meta_key as by using delete_post_meta_by_key('like_count' ); 2. If not then you can create your own table and on un-installation of plugin you can delete that. Apr 1, 2016 at 5:44
  • @EralpŞahin Check this for more reference : Function Reference/delete post meta. Apr 1, 2016 at 5:56
0

The post meta (also known as custom fields) helps us to associate more content with a post. This content could be anything you want to associate with your post, in your case "likes".
So yes your wp_postmeta table should get modified when you use update_post_meta function.

This is the structure of the wp_postmeta table

CREATE TABLE IF NOT EXISTS `wp_postmeta` (
  `meta_id` bigint(20) unsigned NOT NULL,
  `post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `meta_value` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

meta_id is just an internal id
post_id will be your post id that your adding your meta data or extra info to.
meta_key will be the key of your new info in your case will be something like 'likes'
meta_value will be the value in your case will be the user_id

So for any post you could have more than one row with likes, each row for each user that like the post.

Or if you just want to save the number of likes and don't care for the user. You can use a meta key like you said "like_count" and just update that value every time someone likes the post.

Your Answer

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

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