Skip to content

Naeem Mahmood

Site Reliability Engineer

  • Home
  • About me
  • Get in touch

WordPress Post Custom Fields (Post Meta)

Posted on April 26, 2013April 26, 2013 by Naeem

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.

Tagged as: custom fields, post meta, wordpress

Categorized in: WordPress

Post navigation

Create a Custom Page Template in WordPress
Google Introduce A Tool For Data Control After Death
  • Home
  • About me
  • Contact
  • Home
  • About me
  • Get in touch

Recent Posts

  • Kubernetes Build and Deploy NodeJS Application and Scale It
  • Centralized vs. Decentralized Configuration Management: Using Ansible for Both
  • Progressive web apps – offline web applications using service worker
  • Transfer Google Drive data between accounts using Google scripts
  • ECMAScript Harmony – Compile ES6 code to ES5 using Traceur/Babel

Archives

  • January 2025
  • May 2017
  • January 2016
  • December 2015
  • November 2015
  • June 2015
  • July 2014
  • April 2014
  • October 2013
  • June 2013
  • April 2013
  • March 2013
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • January 2012
  • September 2011
search engine optimization