I think I'm echoing the value of the selected option from my select meta box wrongly. Nothing is being shown in the browser.

Functions.php :

function add_custom_meta_box() {
    add_meta_box(
        'custom_meta_box',
        'Custom Meta Box', 
        'show_custom_meta_box', 
        'post', 
        'normal', 
        'high'); 
}
add_action('add_meta_boxes', 'add_custom_meta_box');



$prefix = 'custom_';
$custom_meta_fields = array(
    array(
        'label'=> 'Map Icon',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'select',
        'type'  => 'select',
        'options' => array (
            'one' => array (
                'label' => 'Option One',
                'value' => 'three'
            ),
            'two' => array (
                'label' => 'Option Two',
                'value' => 'two'
            ),
            'three' => array (
                'label' => 'Option Three',
                'value' => 'three'
            )
        )
    )
);

 function show_custom_meta_box() {
global $custom_meta_fields, $post;

echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';


    echo '<table class="form-table">';
    foreach ($custom_meta_fields as $field) {

        $meta = get_post_meta($post->ID, $field['id'], true);

        echo '<tr>
                <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                <td>';
                switch($field['type']) {

                    case 'select':
                        echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';

                        foreach ($field['options'] as $option) {
                            echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
                        }

                        echo '</select><br /><span class="description">'.$field['desc'].'</span>';
                    break;
                } 
        echo '</td></tr>';
    } 
    echo '</table>'; 
}


function save_custom_meta($post_id) {
    global $custom_meta_fields;


    if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
        return $post_id;

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $post_id;

    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 ($custom_meta_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);
        }
    } 
}

add_action('save_post', 'save_custom_meta');  

front-page.php:

Copied the get_post_meta part from the codex, but doesn't seem to be working in the loop i'm using it in:

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'be_home_loop' );
function be_home_loop() {
 echo '<div id="pagewrap"> ';
echo '<div id="content">';
while (have_posts()) : the_post();
    ?>
        <div class="entry">
        <?php the_title( '<h3>', '</h3>' ); ?>
            <?php the_content('read more &raquo;'); 
           $key_2_value =  get_post_meta( get_the_ID(), 'custom_meta_box', true );
  if( ! empty( $key_2_value )) {
  echo $key_2_value;
  }
  • 1
    I'm really tired, but here goes from what I can see. Your query is wrong. while (have_posts()) : the_post(); should be while ($my_query->have_posts()) : $my_query->the_post(); – Pieter Goosen Dec 3 '14 at 19:10
  • @PieterGoosen Thank-you, I deleted the $my_query line, that shouldn't have been there. I have found an answer now (below), but don't understand why the code above doesn't work still. Thanks so much!! – mattnewbie Dec 3 '14 at 20:06
up vote 2 down vote accepted

I got your "broken" code in your answer to work and will explain why. First, here's the edited code that wasn't working for you:

<?php

    //Get your post ID with a variable
    $this_post_id = get_the_ID();

    //Removed get function from get_post_meta to make functionality clearer
    //I tend to avoid nesting functions that are grabbing post information for easier debugging/reading
    //You need to use the field ID as the parameter in get_post_meta, what you had was the meta box id, not the field itself
    //Therefore WordPress didn't know what to retrieve since the metabox itself wasn't the custom meta you were grabbing
    $key_2_value =  get_post_meta( $this_post_id, 'custom_select', true );

    //Debugging got you an empty string before, uncomment this to see the results now
    //var_dump($key_2_value);

    if( ! empty( $key_2_value )) {
        //This works now - yay!
        echo $key_2_value;
    } ?>

The first thing I did was avoid nesting functions within functions so I made get_the_ID into a variable as well. It makes for easier reading and debugging should anything happen because then you can run var_dump on your variables piece by piece to confirm you're getting all the information you need when troubleshooting.

Running var_dump on $key_2_value gave me an empty string. I ran var_dump on $this_post_id which was working for the post ID, cool, then I realized something.

I changed this:

get_post_meta( get_the_ID(), 'custom_meta_box', true );

To this:

get_post_meta( $this_post_id, 'custom_select', true );

If you look at your functions file, in your $custom_meta_fields definition, you gave the select box an ID of "custom_select":

$prefix = 'custom_';    
$custom_meta_fields = array(
    array(
        'label'=> 'Map Icon',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'select', //right here!
        'type'  => 'select',
        'options' => array (
            'one' => array (
                'label' => 'Option One',
                'value' => 'three'
            ),
            'two' => array (
                'label' => 'Option Two',
                'value' => 'two'
            ),
            'three' => array (
                'label' => 'Option Three',
                'value' => 'three'
            )
        )
    )
);

So with get_post_meta, the $key parameter is the ID of the post meta field. And so you had the name of your meta box, custom_meta_box there instead of the name of the actual field itself. Once I changed that to custom_select, it worked.

Hope that helped! :-)

The following works:

$post_meta_data = get_post_custom($post->ID);
echo $post_meta_data['custom_select'][0];

Please can someone explain to me why this doesn't work? I know it doesn't, but not sure why

 $key_2_value =  get_post_meta( get_the_ID(), 'custom_meta_box', true );
  if( ! empty( $key_2_value )) {
  echo $key_2_value;
  • 1
    Do the a var_dump() to see what output you get from your variables and work according to that :-) – Pieter Goosen Dec 4 '14 at 7:17
  • 1
    @mattnewbie I provided an answer explaining how to troubleshoot this and how I fixed it. Please take a look and let me know if it helps. Thanks. – RachieVee Dec 4 '14 at 19:42
  • @PieterGoosen Thank-you, I used var_dump() it's very useful. I shall be using it a lot . Thank you so much!! – mattnewbie Dec 5 '14 at 11:27
  • @RachieVee It makes perfect sense now. Really appreciated thank-you!! – mattnewbie Dec 5 '14 at 11:28
  • @mattnewbie glad you got this solved. I abuse var_dump(). Without knowing a structure of a variable is like driving blindfolded. :-) . Enjoy – Pieter Goosen Dec 5 '14 at 11:50

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.