0

I am trying to add some data in WordPress Custom Field and via adding more direct records/data in wp_postmeta table via SQL query. There are four column as mentioned below...

  • 'meta_id' - A unique id for each entry.
  • 'post_id' - The ID of the post for this metadata.
  • 'meta_key' - The name of the 'key'.
  • 'meta_value' - The value associated with the key.

Now I am using the below SQL query to add some more records/data.

$INSERTING_DATA = "INSERT INTO `wp_postmeta` (`meta_id`, `post_id`, `meta_key`, `meta_value`) values ('XXXXXXXXX', 'WordPress_Post_ID_No', 'WordPress_Post_Custom_Field_Name', 'WordPress_Post_Custom_Field_Value')";

So here I am able to add all my data but I want to know that what to add in meta_id column ? Is that auto increment value so I have to leave that value and should I use below SQL query...???

$INSERTING_DATA = "INSERT INTO `wp_postmeta` (`post_id`, `meta_key`, `meta_value`) values ('WordPress_Post_ID_No', 'WordPress_Post_Custom_Field_Name', 'WordPress_Post_Custom_Field_Value')";

So Now tell me which one is correct and Ok to use. First or second...??? Also describe me that what is meta_id in this table?

  • 3
    Is there a particular reason you aren't using the add_post_meta function? – Tom J Nowell Sep 24 '14 at 17:40
  • @TomJNowell I am doing something different. Not in WordPress but from outside of WordPress... – Muhammad Hassan Sep 25 '14 at 14:15
2

meta_id in postmeta table is just an AUTO_INCREMENT id for the table, you can easily inspect that:
meta_id - AUTO INCREMENT

So, your guess is correct, you can leave it.

EDIT

Avoid raw SQL statements and introduce $wpdb for SQL purposes. And for postmeta insertion you can see the following tutorial I found a best one:

  • Thanks for confirming the answer... This is it what I want to confirm...I am hitting WP database from outside the WordPress so this is why I am asking this... – Muhammad Hassan Sep 25 '14 at 14:18
2

You should not be using a direct SQL. Consider creating a PHP script which loads WordPress core and uses proper functions (see https://stackoverflow.com/questions/9101503/include-wordpress-core-into-own-scripts#answer-9126230 for getting an idea how to do that).

But if you do want to create custom SQL (if you are asking the question you are asking I highly do not recommend it), you should probably get more information on postmeta table before doing so. Let me cover the topic for you.

First of all, let's get some overview of the table:

mysql> describe wp40.wp_postmeta;
+------------+---------------------+------+-----+---------+----------------+
| Field      | Type                | Null | Key | Default | Extra          |
+------------+---------------------+------+-----+---------+----------------+
| meta_id    | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| post_id    | bigint(20) unsigned | NO   | MUL | 0       |                |
| meta_key   | varchar(255)        | YES  | MUL | NULL    |                |
| meta_value | longtext            | YES  |     | NULL    |                |
+------------+---------------------+------+-----+---------+----------------+
4 rows in set (0,08 sec)

From that you can see that meta_id is auto_increment and thus MySQL will figure out itself what value it should add. Passing simple 0 instead of real meta_id is enough.

post_id refers back to it's posts to which the meta is associated. Default is 0 but you should pass a real post_id so you can see the mate in custom fields editor on post editing screen.

meta_key is the name of a meta. Pay attention, using a name starting with underscore eg.: _hidden_meta will hide the meta from custom field editor on edit screen.

meta_value is a longtext and can hold any text you need. But pass it strings only! (in case of array or other object, you can serialize the text).

But once again, consider using add_post_meta or update_post_meta functions.

  • well explained the causes. nice one. – Mayeenul Islam Sep 24 '14 at 18:08
  • Thanks for explaining and clear out my confusion. I am hitting WP database from outside the WordPress so this is why I am asking this... – Muhammad Hassan Sep 25 '14 at 14:19

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.