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.

I have a series of input fields sets:

<input type="text" name="people[name]" />
<input type="text" name="people[lastname]" />
<input type="text" name="people[email]" />

<input type="text" name="people[name]" />
<input type="text" name="people[lastname]" />
<input type="text" name="people[email]" />

...

and I am looking for a way of storing the values as an array like the one following

{ people: { name: xxx , lastname: yyy, email: zzz }, 
          { name: xxx , lastname: yyy, email: zzz },
          { name: xxx , lastname: yyy, email: zzz },
          ...
}

I will use update_post_meta(); to save the values.

Thank you for your help.

EDIT

// CALLBACK
<?php
function show_people_box($post){
$metas = json_decode(get_post_meta($post->ID,'people',true));
?>
<?php 
$hiterms = get_terms('people', array('orderby' => 'slug', 'parent' => 0, 'hide_empty' =>false)); 

?>
<?php foreach($hiterms as $key => $hiterm) { ?>
<h2 style="text-align:center;"><?php echo $hiterm->name; ?></h2>
<table width="100%">
<?php
$args = array(
    'post_type'=>'people',
    'orderby' => 'menu_order',
    'post_parent' => 0,
    'tax_query' => array(array(
    'taxonomy' => 'people',
    'field' => 'id',
    'terms' => $hiterm->term_id
)));
?>
<?php 
$query = new WP_Query($args);
while($query->have_posts()){
$query->the_post();
?>
<?php 
$children = get_children(array('post_type' => 'people', 'post_parent' => get_the_ID() ));
if($children) {
?>
<tr>
    <td><input name="people[name]" type="text" /></td>
    <td><input name="people[lastname]" type="text" /></td>
    <td><input name="people[email]" type="text" /></td>
</tr>
<?php

$args_2 = array( 'post_type'=>'people','post_parent' => get_the_ID() );
$query_2 = new WP_Query($args_2);
while($query_2->have_posts()){
$query_2->the_post();
?>
<tr>
    <td><input name="people[name]" type="text" /></td>
    <td><input name="people[lastname]" type="text" /></td>
    <td><input name="people[email]" type="text" /></td>
</tr>
<?php
}
} else {

?>
<tr>
    <td><input name="people[name]" type="text" /></td>
    <td><input name="people[lastname]" type="text" /></td>
    <td><input name="people[email]" type="text" /></td>
</tr>
<?php 
}
?>
<?php
} wp_reset_postdata(); 
?>
</table>
<?php } ?>
<?php
}

// SAVE
add_action( 'save_post', 'save_people_box' );
function save_people_box(){

foreach($_POST['people'] as $key => $value){
$peoples[$key] = $value;
}

global $post;
update_post_meta( $post->ID, 'people', json_encode($peoples) );

}

I hope it helps, in my attempt to simplify my question I removed some html tags. Thanks for all your help again.

share|improve this question
add comment

1 Answer

create an array from your form fields $_POST['people'] data use foreach loop to get and create array of $_POST['people'] data and then save array of data in post meta filed, do so like this

foreach ($_POST['people'] as $key => $value) {
    $peoples[$key] = $value;
}

update_post_meta($post_id, 'peoples', $peoples);
share|improve this answer
    
Please explain this answer in more detail. Why does this work, for example? –  s_ha_dum Sep 12 '13 at 17:23
    
@s_ha_dum asker want to create an array from form fields and want to save in post meta as an array. –  Anjum Sep 12 '13 at 17:39
    
I know what the OP wants. Summarizing the question doesn't explain the answer. –  s_ha_dum Sep 12 '13 at 17:40
    
@Anjum thanks for your answer, it works perfectly when I don't provide names to my fields (ex: people[]) but when I do it (people['name']) saves just the last item of the array. Am i doing something wrong? –  Efrain Sep 12 '13 at 18:22
    
can you paste your complete code here. –  Anjum Sep 12 '13 at 19:18
show 3 more comments

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.