Custom fields are used to save additional data about Posts/Articles. This is very common feature among all popular Content management systems like Drupal, Joomla etc. In Drupal additional data fields are saved for specific node type and same is for Joomla. In WordPress it is already very mature and easy to use, WordPress has powerful functions which can be used to saved, update and retrieve Custom field values and meta. WordPress adds a custom field (also called meta-data) to a specified post which could be of any post type.
[php]<?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?>[/php]
Its just easy to save a custom field using above function add_post_meta() which can be retrieve later using another function get_post_meta(). these both functions are part of core file wp-includes/post.php.
In function add_post_meta first parameter is $post_id, second is $meta_key (which should be unique and will be used to retrieve meta-data), third is its value (meta value) and fourth one is $unique parameter, When set to true, the custom field will not be added if the given key already exists among custom fields of the specified post. in that case update_post_meta() function can be used.
Function delete_post_meta() is used to delete specific Custom field. These are basic function which are used for crud operations but there are also other supporting function which are used.
- get_post_custom
- get_post_custom_keys
- get_post_custom_values
get_post_custom returns a multidimensional array with all custom fields of a particular post or page.
[php]<?php
$custom_fields = get_post_custom(22);
$my_custom_field = $custom_fields[‘my_custom_field’];
foreach ( $my_custom_field as $key => $value )
echo $key . " => " . $value . "<br />";
?>[/php]
get_post_custom_keys simply return array of keys of specific post type and same way get_post_custom_values is used to retrieve array of values.
So in this way use of WordPress custom field make WordPress more powerful CMS, you can used to save any kind of data for specific post. In WordPress popular eleven table schema, One table is reserved for post-meta.