Title: Multiple Custom Fields
Last modified: August 19, 2016

---

# Multiple Custom Fields

 *  [Lomaski](https://wordpress.org/support/users/lomaski/)
 * (@lomaski)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/multiple-custom-fields-4/)
 * I was asked to do a post_type which will have unknown number of fields, so i 
   did it with jQuery and the jQuery works fine but it’s not updating the fields
   in the DB, help please?
 * The function.php part
 *     ```
       function post_type_lookbook() {
       	$support = array('title','author');
       	register_post_type( 'lookbook',
       		array( 'label' => __('LookBook'),'taxonomies' => array('category', 'post_tag'), 'public' => true, 'show_ui' => true,  'query_var' => true, 'supports' => $support, 'menu_position' => 5,'capability_type' => 'post','rewrite' => array( 'slug' => 'lookbook' ) ) );  
   
       }
   
       add_action('save_post', 'save_price2');
           function meta_options2(){
               global $post, $meta_boxes;
               $custom = get_post_custom($post->ID);
       		$countm = $custom["_countm"][0];
       		for($i=0,$i<$countm;$i++;){
               	$male.$i = $custom["_male".$i][0];
       		}
   
       // look up for the path
   
       global $wpdb;
   
       ?>
           <div class="form-field male"><label>Masculino:<input class="male" name="male0" rel="0" value="<?php echo $countm; ?>" /><input name="countm" class="count" type="hidden" value="<?php echo $countm;?>" /></label></div>
           <br /><a class="mais" href="#">Adicionar mais um campo</a><br /><br />//the link that adds another field(s)
       <?php
           }
   
       function save_price2(){
           global $post;
       	update_post_meta($post->ID, "_countm", $_POST["countm"]);
       	for($i=0,$i<$_POST['countm'];$i++;){
           	update_post_meta($post->ID, "_male".$i."", $_POST["male".$i]);
       	}
   
       }
       ```
   
 * the jQuery script
 *     ```
       jQuery(document).ready(function() {
       jQuery('.mais').live("click", function(){
   
       	var numero = jQuery('.male:last').attr('rel');
   
       	prox = parseInt(numero);
   
       	prox = prox+1;
   
       		jQuery('.male:last')
   
       		.clone()
   
       		.appendTo('.form-field.male')
   
       		.attr('rel', prox)
   
       		.attr('name', 'male'+prox)
   
       		jQuery('.male:last').val('');
   
       		jQuery('.count').val(prox);
   
       	});
       });
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [Lomaski](https://wordpress.org/support/users/lomaski/)
 * (@lomaski)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/multiple-custom-fields-4/#post-1857118)
 * well since nobody answered this i’ve made some changes to the code by myself 
   and now I have this problem: it doesn’t save the value in the database
    the php
   code:
 *     ```
       add_action('save_post', 'save_price2');
           function meta_options2(){
               global $post, $meta_boxes;
               $custom = get_post_custom($post->ID);
       		$countm = $custom["_countm"][0];
       		for($i=0;$i<=$countm;$i++){
       			$male[$i] = $custom["_male[".$i."]"][0];
       		}
   
       		$countf = $custom["countf"];
       		for($i=0;$i<=$countm;$i++){
       			$female[$i] = $custom["_female[".$i."]"][0];
       		}
   
       // look up for the path
   
       global $wpdb;
   
       ?>
           <div class="form-field male"><label>Masculino:
           <?php for($i=0;$i<=$countm;$i++){?>
           	<input class="male" name="male[<?php echo $i;?>]" rel="0" value="<?php echo $male[$i]; ?>" />
           <?php ;}?>
           <input name="countm" class="countm" type="hidden" value="<?php echo $countm;?>" />
           </label></div>
           <br /><a class="maish" href="#">Adicionar mais um campo</a><br /><br />
           <div class="form-field female"><label>Feminino:
           <?php for($i=0;$i<=$countf;$i++){?>
           <input class="female" name="female[<?php echo $i;?>]" rel="0" value="<?php echo $female[$i]; ?>" />
           <?php ;}?>
           <input name="countf" class="countf" type="hidden" value="<?php echo $countf;?>" />
           </label></div>
           <br /><a class="maism" href="#">Adicionar mais um campo</a><br /><br />
   
       <?php
           }
   
       function save_price2(){
           global $post;
       	update_post_meta($post->ID, "_countm", $_POST["countm"]);
       	for($i=0;$i<$_POST["countm"];$i++){
       		update_post_meta($post->ID, "_male[".$i."]", $_POST["male[".$i."]"]);
       	}
       	update_post_meta($post->ID, "_countf", $_POST["countf"]);
       	for($i=0;$i<$_POST["countf"];$i++){
       		update_post_meta($post->ID, "_female[".$i."]", $_POST["female[".$i."]"]);
       	}
   
       }
       register_taxonomy_for_object_type( 'post_tag', 'lookbook');
       ```
   
 * the jquery code
 *     ```
       jQuery(document).ready(function() {
       jQuery('.maish').live("click", function(){
   
       	var numero = jQuery('.male:last').attr('rel');
   
       	if(numero<59){
   
       	prox = parseInt(numero);
   
       	prox = prox+1;
   
       		jQuery('.male:last')
   
       		.clone()
   
       		.appendTo('.form-field.male')
   
       		.attr('rel', prox)
   
       		.attr('name', 'male['+prox+']')
   
       		.focus()
   
       		jQuery('.male:last').val('');
   
       		jQuery('.countm').val(prox);
   
       	}else{
       		jQuery('.maish').empty();
       	}});
       jQuery('.maism').live("click", function(){
   
       	var numero = jQuery('.female:last').attr('rel');
   
       	if(numero<59){
   
       	prox = parseInt(numero);
   
       	prox = prox+1;
   
       		jQuery('.female:last')
   
       		.clone()
   
       		.appendTo('.form-field.female')
   
       		.attr('rel', prox)
   
       		.attr('name', 'female['+prox+']')
   
       		.focus()
   
       		jQuery('.female:last').val('');
   
       		jQuery('.countf').val(prox);
   
       	}else{
       		jQuery('.maism').empty();
       	}});
       });
       ```
   
 * now anyone can help?

Viewing 1 replies (of 1 total)

The topic ‘Multiple Custom Fields’ is closed to new replies.

## Tags

 * [jquery](https://wordpress.org/support/topic-tag/jquery/)
 * [post_type](https://wordpress.org/support/topic-tag/post_type/)
 * [update_post_meta](https://wordpress.org/support/topic-tag/update_post_meta/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Lomaski](https://wordpress.org/support/users/lomaski/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/multiple-custom-fields-4/#post-1857118)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
