Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I store multiple values in one meta_key through a radio input and then display the two values on the page? One value would be a text string and the other would be a link that would go with that text. These values would not change and would be populated by the data in the radio box.

Here is the code for my meta boxes. I want the radio box event_venue to have an additional value that would be the link to the venue page. That way I would just have to click the radio button for the venue and the name and link to the page would automatically show up.

 $meta_box['event'] = array(
    'id' => 'event-meta-details',
    'title' => 'Event Information',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Start Date',
            'desc' => '(Enter yyyy/mm/dd)',
            'id' => 'start_date',
            'type' => 'text',
            'default' => ''
        ),
        array(
            'name' => 'End Date',
            'desc' => '(Enter yyyy/mm/dd)',
            'id' => 'end_date',
            'type' => 'text',
            'default' => ''
        ),
        array(
            'name' => 'Opening',
            'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
            'id' => 'opening_time',
            'type' => 'text',
            'default' => ''
        ),
           array(
            'name' => 'Closing',
            'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
            'id' => 'closing_time',
            'type' => 'text',
            'default' => ''
        ),
        array(
            'name' => 'Artist Talk - Lecture',
            'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
            'id' => 'artist_talk_time',
            'type' => 'text',
            'default' => ''
        ),
        array(
        'name' => 'Venue',
        'desc' => 'Venue of Event',
        'id' => $prefix . 'event_venue',
        'type' => 'radio',
        'options' => array(
            array('name' => 'William Busta Gallery', 'value' => 'William Busta Gallery'),
            array('name' => 'Spaces', 'value' => 'Spaces'),
            array('name' => 'Enter Text Below', 'value' => 'other')
        )
    ),
        array(
            'name' => 'Custom Venue',
            'desc' => 'Enter Venue Name if Enter Text Below is Checked',
            'id' => 'custom_event_venue',
            'type' => 'text',
            'default' => ''
        ),
        array(
                'name' => 'Featured / Reccomended',
            'desc' => 'Recommended Event',
            'id' => 'featured_event',
            'type' => 'checkbox',
            'default' => ''
          )

    )
);

Right now I display the venue value like this.

if (get_post_meta(get_the_ID(),'event_venue', true) != 'other') { 
    echo get_post_meta(get_the_ID(),'event_venue', true);
    }
if (get_post_meta(get_the_ID(),'event_venue', true) == 'other') {
    echo get_post_meta(get_the_ID(),'custom_event_venue', true);
    }

This is the code that saves the data.

// Save data from meta box
function plib_save_data($post_id) {
    global $meta_box,  $post;

    //Verify nonce
    if (!wp_verify_nonce($_POST['plib_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[$post->post_type]['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', 'plib_save_data');

Thank you in advance for any advice. I am very confused.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

John,

I'm not sure how you have the radio button set up, but WordPress easily handles multiple values in one meta_key. You simply need to place the values in an array before sending it to update_post_meta. For instance:

$array = array( 'foo' => 'foo_value', 'bar' => 'bar_value' );
update_post_meta( $id, 'my_meta_key', $array );

WordPress automatically serializes this array on the way in and unserializes it on the way out.

My personal recommendation would be to save the two pieces of data as separate pieces of metadata. By serializing the array and sending it to the database, you lose the ability to query for the individual piece of information.

Finally, you should make sure to sanitize/validate your data when saving it to the database and escape it on display. The Codex has a nice article on how to do this: http://codex.wordpress.org/Data_Validation

share|improve this answer
    
Thank you for the reply. I think I will keep the data separate. Now I need to figure out how to automatically populate a series of radio buttons or a drop down list automatically with all of the links to a certain page category. –  John Bentwin Dec 8 '11 at 14:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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