I have created a custom meta box which I have pasted in functions.php (minus the <?php call of course)

I have two fields in this meta box, one is labeled 'Primary Title' and the other is labeled 'Secondary Title'.

How do I call data from each of these boxes individually so that I can insert them into the template of a page?

$prefix = 'dbt_';
$meta_box = array(
  'id' => 'my-meta-box',
  'title' => 'Custom meta box',
  'page' => 'post',
  'context' => 'normal',
  'priority' => 'high',
  'fields' => array(
     'name' => 'Primary Title',
     'id' => $prefix . 'text',
     'type' => 'text'
     'name' => 'Secondary Title',
     'id' => $prefix . 'text1',
     'type' => 'text',  

add_action('admin_menu', 'mytheme_add_box');

// Add meta box
function mytheme_add_box() {
  global $meta_box;

// Callback function to show fields in meta box
function mytheme_show_box() {
  global $meta_box, $post;
  // Use nonce for verification
  echo '<input type="hidden" name="mytheme_meta_box_nonce" value="';
  echo wp_create_nonce(basename(__FILE__)), '" />';
  echo '<table class="form-table">';
  foreach ($meta_box['fields'] as $field) {
    // get current post meta data
    $meta = get_post_meta($post->ID, $field['id'], true);   
    echo '<tr><th style="width:20%"><label for="' . $field['id'] . '">';
    echo $field['name'];
    echo '</label></th><td>';
    switch ($field['type']) {
      case 'text':
        echo '<input type="text" name="' . $field['id'] . '"';
        echo ' id="' .$field['id'] . '"';
        echo ' value="' . ( $meta ? $meta : $field['std'] ) . '"';
        echo ' size="30" style="width:97%" />';
        echo '<br />' . $field['desc'];
    echo '<td></tr>';
  echo '</table>';

add_action('save_post', 'mytheme_save_data');

// Save data from meta box
function mytheme_save_data($post_id) {
    global $meta_box;
    // verify nonce
    if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
      return $post_id;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
      return $post_id;
    // check permissions
    if ('page' == $_POST['post_type']) {
      if (!current_user_can('edit_page', $post_id)) {
        return $post_id;
    } elseif (!current_user_can('edit_post', $post_id)) {
      return $post_id;
    foreach ($meta_box['fields'] as $field) {
      $old = get_post_meta($post_id, $field['id'], true);
      $new = $_POST[$field['id']];  
      if ($new && $new != $old) {
        update_post_meta($post_id, $field['id'], $new);
      } elseif ( '' == $new && $old ) {
        delete_post_meta($post_id, $field['id'], $old);
  • Please edit relevant code into the question. A question should not depend upon data from a third party.
    – s_ha_dum
    Apr 1 '14 at 15:40
  • Updated accordingly. Apr 1 '14 at 15:53

Meta boxes are saving data at each post ($post_id)

$value = get_post_meta($post_id, 'name', true);

name = 'dbt_text' or 'dbt_text1'


$value = get_post_meta($post_id, 'dbt_text', true);
$value1 = get_post_meta($post_id, 'dbt_text1', true);

~ more info on get_post_meta

  • For some reason, this isn't fetching my information. Apr 6 '14 at 21:55
  • @AndrettiMilas this line: $meta = get_post_meta($post->ID, $field['id'], true) comes from your code. If your metabox works well, the code in this answer have to work too. So be sure that when you call it what you use as 1st argument for get_post_meta is the ID of post where you saved the fields and the 2nd argument ('dbt_text' or 'dbt_text1' in the answer) match exactly what you have in $field['id'], including prefix.
    – gmazzap
    Apr 7 '14 at 0:31
  • I tried exactly what was in the answer and it did not work. Apr 7 '14 at 3:17
  • @AndrettiMilas You must define $post_id. Try this: echo get_post_meta( get_the_ID(), 'dbt_text', true ) in the single post loop.
    – birgire
    Apr 7 '14 at 7:31

I just ran your initial code myself with wp_debug on and it seems that there is some basic housekeeping needed for this to actually save the data into the custom field. I think your problem so far is that there is no data being saved in, therefore you cannot read anything out.

On line 55 and 57 you are trying to prints parts of the array that do not exist, which is leading to some errors.

Line 55:

echo ' value="' . ( $meta ? $meta : $field['std'] ) . '"';

$field['std'] does not exist, the only keys inside of $field are name, id and type.

Line 57:

echo '<br />' . $field['desc'];

$field['desc'] also does not exist. If you want these keys to exist in the array, add them in your metabox array initially.

I changed the line 55 to reference $field['id'] and removed line 57 and it works just fine.

Then, in your template file if you call:

$value = get_post_meta($post->ID, 'dbt_text', true);

$value1 = get_post_meta($post->ID, 'dbt_text1', true);

echo $value;

You will see your correct data.

  • @AndrettiMilas are you still looking for a solution to this? If not, please mark it answered. Apr 11 '14 at 18:16

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.