In my code, I enhance one of my content types by following code:
add_post_meta( $post->ID, 'favourite_fruit_code', $value )
All goes well, but I would like to have power over specifying what a column type I would like to create in DB.
There are cases when we would need different DB column types would be appropriate - depending on data we are going to store. How can I specify this? (For example if I want my field to be stored in DB as TinyIntegerField
, or BigIntegerField
, etc.).
In my code, I enhance one of my content types by following code:
add_post_meta( $post->ID, 'favourite_fruit_code', $value )
All goes well, but I would like to have power over specifying what a column type I would like to create in DB.
There are cases when we would need different DB column types would be appropriate - depending on data we are going to store. How can I specify this? (For example if I want my field to be stored in DB as TinyIntegerField
, or BigIntegerField
, etc.).
When you use add_post_meta
, no column in database is created. All post meta data is stored in wp_postmeta
table (https://codex.wordpress/Database_Description#Table:_wp_postmeta). Here's the structure of that table:
post_id
is the ID of post that this meta data is assigned tometa_key
is the key of meta (in your case favourite_fruit_code
)meta_value
is the value of that metaSo you're not able to set the type of column, because there is no column, and you're not able to set the type of value, because all values are stored as LONGTEXT.
On the other hand, there is no need for setting the type of this column. You should sanitize and validate the values before setting them, and you can use meta_type
in your meta query, so the values are compared correctly.