0

I got a custom post type, with a form for storing some data (name, url) to display in a template.

What I want to know is how can I store those values in an array?

An example of my code:

<? function files_metadata(){  
        global $post; 
        $custom = get_post_custom($post->ID);  
        $name = $custom["name"][0];
        $url = $custom["url"][0];

        echo '<input type="hidden" name="files_metadata" id="files_metadata" value="' .wp_create_nonce('files_m'). '" />'; ?>  

<label>Name: </label><br/>
<input id="name" name="name" value="<?php echo $name; ?>" />
<label>Url: </label><br/>
<input id="url" name="url" value="<?php echo $url; ?>" />

<? function save_meta_files($post_id) {  
        if (!wp_verify_nonce($_POST['files_metadata'], 'files_m')) return $post_id;
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;

        update_post_meta($post_id, "name", $_POST["url"]);   
        update_post_meta($post_id, "url", $_POST["url"]); 
    }  

add_action('save_post', 'save_meta_files'); ?> 

To this I want to add something like...

$url = $custom["url"][0];
$url2 = $custom["url"][1];
$url3 = $custom["url"][2];

<input id="url" name="url[0]" value="<?php echo $url; ?>" />
<input id="url2" name="url[1]" value="<?php echo $url2; ?>" />
<input id="url3" name="url[2]" value="<?php echo $url3; ?>" />

update_post_meta($post_id, "url", $_POST["url"][0]); 
update_post_meta($post_id, "url2", $_POST["url"][1]); 
update_post_meta($post_id, "url3", $_POST["url"][2]); 

...but that actually works...

2
  • I don't really understand what you are trying to do. Commented Jun 1, 2011 at 1:52
  • Thanks for answer, I want to store values in arrays, like the ones in the second code box, but the problem that I got is that the values are not stored with the wordpress code, I guess something is wrong with it... can you please check it out? Commented Jun 1, 2011 at 14:22

1 Answer 1

2

A passed array will be serialized into a string: http://codex.wordpress.org/Function_Reference/update_post_meta

update_post_meta(
    $post_id,
    'files_metadata',
    array(
        'name1' => $_POST['name1'],
        'url1' => $_POST['url1']
        'name2' => $_POST['name2'],
        'url2' => $_POST['url2']
    )
);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer, but for more that I try, I couldn't get right the other two where is $custom = get_post_custom($post->ID); $name = $custom["files_metadata"][0]; and <input id="name" name="files_metadata[0]" value="<?php echo $name; ?>" />, what could be the right way to put it?
Take a look at the structure of the output of var_dump(get_post_custom($post->ID));.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.