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

2 Answers 2

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

If two fields are named people[name], only the last value will be sent with the form. To be completely reliable, you'll need to use an id for each person you're collecting details on:

<input name="people[0][name]">
<input name="people[0][email]">
<input name="people[1][name]">
<input name="people[1][email]">
...

Use an incrementing counter in your while loop to do this ($i):

<?php
$i = 0;
while ( $posts->have_posts() ) {
    ...
    ?>
    <input name="people[<?php echo $i; ?>][name]">
    <?php
    $i++;
endwhile;
?>

You can use the empty array key format ([]) only if you don't need to link other fields together.

<input name="people[name][]" type="text">

You'll only receive non-empty field values, so empty fields will get tossed and your array key will get messed up. Consider the following situation:

<input name="people[name][]">
<input name="people[email][]">
<input name="people[name][]">
<input name="people[email][]">

If a user only filled out the second name field, the second name would have an array key of 0. You would have no way of linking the second name to the second email.

In the following situation:

<input name="people[][name]">
<input name="people[][email]">

Each new named field will be given a unique key in the index, so the email field would be at $_POST[1][email].

share|improve this answer

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.