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.

UPDATED CODE 12/12/2011

Here is the code in functions.php. I put the code for output in my events page. For now there has been on output.

<?php
// Custom Meta Data for Events
   $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' => 'Venue',
        'desc' => 'Venue of Event',
        'id' => $prefix . 'event_venue',
        'type' => 'radio',
        'options' => array(
            array('name' => 'Spruce Gallery', 'value' => 'Spruce  Gallery'),
            array('name' => 'Pine Gallery', 'value' => 'Pine Gallery'),
            array('name' => 'Oak Gallery', 'value' => 'Oak Gallery')
        )
    )

    )
);


///////////////
 function get_related_event_data( $venue, $field = 'all' ) {
$event_data = array(
    'Spruce Gallery' => array(
        'address' => 'Test Address 1',
        'phone_no' => '344-333-2333',
        'url' => 'http://www.url1.com'
    ),
    'Pine Gallery' => array(
        'address' => 'Test Address 2',
        'phone_no' => '444-333-3333',
        'url' => 'http://www.url2.com'
    ),
    'Oak Gallery' => array(
        'address' => 'Test Address 3',
        'phone_no' => '555-666-6666',
        'url' => 'http://www.url3.com'
    ),
);
if( !isset( $event_data[$venue] ) )
    return;

// Optionally return all fields(note: you can't echo arrays, so be sure to loop over the data if pulling all fields
if( 'all' == $field )
    return $event_data[$venue];

if( !isset( $event_data[$venue][$field] ) )
    return;

return $event_data[$venue][$field];
}

///////////////

Here is the code I put on the page. I put in a test echo to make sure that the $venue was getting information and it is, but the address does not display.

<?php 

$venue = get_post_meta( $post->ID, 'event_venue', true );
echo $venue;

echo get_related_event_data( $venue, 'address' ); ?>

Old Post.

Here is the code that fills in my meta boxes.

 // Custom Meta Data for Events
   $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' => 'Venue',
        'desc' => 'Venue of Event',
        'id' => $prefix . 'event_venue',
        'type' => 'radio',
        'options' => array(
            array('name' => 'Spruce Gallery', 'value' => 'Spruce  Gallery', 'venue_url' => 'http://www.sprucegallery.com/'),
            array('name' => 'Pine Gallery', 'value' => 'Pine Gallery'),
            array('name' => 'Oak Gallery', 'value' => 'Oak Gallery')
        )
    )

    )
);

Here is the code for saving the information.

// Add meta boxes to admin panel only needs to be added once
add_action('admin_menu', 'plib_add_box');

//Add meta boxes to post types
function plib_add_box() {
    global $meta_box;

    foreach($meta_box as $post_type => $value) {
        add_meta_box($value['id'], $value['title'], 'plib_format_box', $post_type,  $value['context'], $value['priority']);
    }
}

//Format meta boxes
function plib_format_box() {
  global $meta_box, $post;

// Use once for verification
echo '<input type="hidden" name="plib_meta_box_nonce" value="',  wp_create_nonce(basename(__FILE__)), '" />';

echo '<table class="form-table">';

foreach ($meta_box[$post->post_type]['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'] .'">'. $field['name']. '</label></th>'.
              '<td>';
      switch ($field['type']) {
          case 'text':
              echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:30%" />'. ' - '. $field['desc'];
              break;
          case 'textarea':
              echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
              break;
          case 'select':
              echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
              foreach ($field['options'] as $option) {
                  echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
              }
              echo '</select>';
              break;
          case 'radio':
              foreach ($field['options'] as $option) {
                  echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
              }
              break;
          case 'checkbox':
              echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' />';
              break;
      }
      echo     '<td>'.'</tr>';
  }

  echo '</table>';

}

// 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');

`I want to save and reuse multiple data points about multiple venues in an event CPT with meta boxes. I have the CPT and meta boxes that can save the event venue that I enter as well as times and dates. The goal is to have a drop down list of venues. When one of the venues is selected then the information for that venue - Address, URL, Phone #, etc. would be selected as well.

I planned to write out the information as an array but am not sure how to do that. Each time I try it doesn't work.

What is the best way to store multiple points of information without having to type them in each time? Worst case I will make a meta_key for each bit of information and enter it by hand, but that seems like pointless data entry. There must be a better way.

Thank you in advance.

share|improve this question
1  
Any chance you can provide some code to go along with what you're describing? I'm having a hard time visualizing how your existing code works and what you're trying to create, it will help visualize what you have and what you're aiming to do.. –  t31os Dec 11 '11 at 18:36
    
@t31os I added some code above. Now if I select a radio button I get a meta_key that can output the name of the venue. I want to select the one radio button and have an array that holds the address, phone number, and URL of the venue. I will not have many new venues so I do not mind typing this out in the function file. I do not need to add the information via Wordpress. Thank you for the help. –  John Bentwin Dec 11 '11 at 22:44
    
So if i'm following, what you're aiming for is to have some relational data displayed dependant on the venue selected? ie. A venue is chosen, and upon that choice display some other data related to the given venue. Should this occur on save or when the venue is selected, ie. live? –  t31os Dec 12 '11 at 10:39
    
@t31os Yes, I want relational data displayed on a page depending on the venue selected. The goal would be for the person entering the event information to enter the date and time, then select the venue -either from a radio button or dropdown. I do not need to display the venue address and info on the admin side just on the front end. For now I figured I would type out an array with all the information for each venue. If the info changed then I would edit the php array file. I think it should occur on save since I do not need the info on the back end. Thank you for the help. –  John Bentwin Dec 12 '11 at 12:25
add comment

1 Answer

up vote 1 down vote accepted

You're already storing the venue, so you should be able to simply extract from an array front-side based on what a get_post_meta call returns. If i'm following you correctly still at this point, something along these lines should do the trick.

function get_related_event_data( $venue, $field = 'all' ) {
    $event_data = array(
        'Spruce Gallery' => array(
            'address' => '',
            'phone_no' => '',
            'url' => ''
        ),
        'Pine Gallery' => array(
            'address' => '',
            'phone_no' => '',
            'url' => ''
        ),
        'Oak Gallery' => array(
            'address' => '',
            'phone_no' => '',
            'url' => ''
        ),
    );
    if( !isset( $event_data[$venue] ) )
        return;

    // Optionally return all fields(note: you can't echo arrays, so be sure to loop over the data if pulling all fields
    if( 'all' == $field )
        return $event_data[$venue];

    if( !isset( $event_data[$venue][$field] ) )
        return;

    return $event_data[$venue][$field];
}

You then pass your meta value into that function to extract the data related to the venue, eg.

$venue = get_post_meta( $post->ID, 'YOURPREFIX_event_venue', true );

echo get_related_event_data( $venue, 'address' );

NOTE: I have no idea how $prefix is defined in your code, so you'd need to replace YOURPREFIX_ with the appropriate prefix(as per your existing code).

If i've not quite followed what you were asking correctly, please do try to clarify what you're aiming for..

Hope that helps.. :)

share|improve this answer
    
Thank you for the detailed reply. $prefix is not defined in the code and I believe is an artifact from a custom post tutorial. I tried your suggested code with and with out the $prefix with no luck. It will not output information from the array. You have what I want. The ability to select a venue when enterind dates and times. Then on the event page I can have the data displayed for the address and phone number. Is pulling from an array in this way the best way to do it even with 80+ venues? Should it be saved with the post or just called from the array when needed? –  John Bentwin Dec 12 '11 at 20:21
    
$venue is getting information since it echos each different venue, but it does not display the address. Thank you again. –  John Bentwin Dec 12 '11 at 20:26
    
Ok... strange behavior. It works for the Pine Gallery and the Oak Gallery but not the Spruce Gallery. I have checked spelling until my eyes hurt. Not sure what the problem is, but the good news is the code is going to work. This is better than typing in all the information. Now I need to figure out what is wrong with the Spruce Gallery and then how to put it into a drop down list that is separated by city. 80 venues from 6 cities in one drop down will work but I would like to make it more efficient. Thank you and hopefully you have some suggestions. –  John Bentwin Dec 12 '11 at 22:34
    
I am not sure why the data does not work with the Spruce Gallery entry. I have changed the order of the array and only Pine and Oak work, but since that is better than what I had before, and since the problem with Spruce is probably something simple that I am doing wrong, I marked the question as answered. Thank you. –  John Bentwin Dec 13 '11 at 12:42
    
Sorry i didn't come back to you sooner, Spruce Gallery has an additional space between the two name parts(i noticed that before but forgot to mention it). Glad to hear you're making progress though.. :) –  t31os Dec 13 '11 at 15:25
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.