Ok, i have a form and a input fields type checkboxes. But in the post appears only the last value, what can be the reason?

<label for="extras"> Extras </label>
  <input type="checkbox" name="extras" value="ABS">Anti blocking system</br>
  <input type="checkbox" name="extras" value="DRS"> Dynamic rear spoiler </br>
  <input type="checkbox" name="extras" value="VSP"> Vibrating selfesteen system

$extras = $_POST['extras']; 

add_post_meta($pid, 'extras', $extras, true); 

//this is in the attempt to be displayed in the post

 $extras = get_post_meta($post->ID, 'extras', false); 
            if ($extras) {
                echo '<ul>';
                    foreach($extras as $extra){
                        echo '<li>' . $extra . '</li>';
                echo '</ul>';
  • This is HTML-related, nothing to do with WP. Here's an answer. – montrealist Mar 26 '14 at 21:11
  • How to insert/add the information from post variable into custom field? Displaying it with get_post_meta() is easy. – worldwildwebdev Mar 26 '14 at 21:45
  • I would suggest re-phrasing your question. It doesn't make much sense to me. – helgatheviking Mar 26 '14 at 23:00
  • Oh sorry. I just added the empty array here and set from 'false' to 'true' and the code goes perfectly. ` <input type="checkbox" name="extras**[]**" value="ABS">Anti blocking system</br> $extras = get_post_meta($post->ID, 'extras', false);` – worldwildwebdev Mar 26 '14 at 23:26

In your code you are only creating one custom field with your add_post_meta() usage. Then when you use the get_post_meta() there is only one value in the post. Can you go into the actual post and look at the custom fields. Is there more than one custom field with the same "extras" key? If not, try an iteration to create multiple custom fields for your post:

$extras = $_POST['extras'];

for ($i = 0, $l = count($_POST['extras']); $i < $l; $i++) 
    add_post_meta($pid, 'extras', $_POST['extras'][$i]), true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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