Skip to main content
edited body
Source Link
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147

I want to optimize my functions of a Custom Post Type (CPT). Now I have 10 meta boxes. For each box I have an extra function. The aim is to produce the 10 boxes in one function. I already tried the following code. The problem is that the entered values do not get saved.

function optimize() 
    {
     global $post;
     $custom = get_post_custom($post->ID);
     for ($i=0; $i<=9; $i++) {
        $field_name[] = $custom['field_name'];
        $checkbox_nr[] = $custom['checkbox_nr'];
        
        echo 'Field Name '.($i).' <input name="field_name' . $i . '" value="' .  $field_name[$i][0] . '" />';       
      }

     update_post_meta($post->ID, "field_name1", $_POST["field_name1"]);
}

I want to optimize my functions of a Custom Post Type (CPT). Now I have 10 meta boxes. For each box I have an extra function. The aim is to produce the 10 boxes in one function. I already tried the following code. The problem is that the entered values do not get saved.

function optimize() 
    {
     global $post;
     $custom = get_post_custom($post->ID);
     for ($i=0; $i<=9; $i++) {
        $field_name[] = $custom['field_name'];
        $checkbox_nr[] = $custom['checkbox_nr'];
        
        echo 'Field Name '.($i).' <input name="field_name' . $i . '" value="' .  $field_name[$i][0] . '" />';       
      }

update_post_meta($post->ID, "field_name1", $_POST["field_name1"]);

I want to optimize my functions of a Custom Post Type (CPT). Now I have 10 meta boxes. For each box I have an extra function. The aim is to produce the 10 boxes in one function. I already tried the following code. The problem is that the entered values do not get saved.

function optimize() {
     global $post;
     $custom = get_post_custom($post->ID);
     for ($i=0; $i<=9; $i++) {
        $field_name[] = $custom['field_name'];
        $checkbox_nr[] = $custom['checkbox_nr'];
        
        echo 'Field Name '.($i).' <input name="field_name' . $i . '" value="' .  $field_name[$i][0] . '" />';       
      }

     update_post_meta($post->ID, "field_name1", $_POST["field_name1"]);
}
Source Link
user51910
user51910

Optimize CPT-function with a loop

I want to optimize my functions of a Custom Post Type (CPT). Now I have 10 meta boxes. For each box I have an extra function. The aim is to produce the 10 boxes in one function. I already tried the following code. The problem is that the entered values do not get saved.

function optimize() 
    {
     global $post;
     $custom = get_post_custom($post->ID);
     for ($i=0; $i<=9; $i++) {
        $field_name[] = $custom['field_name'];
        $checkbox_nr[] = $custom['checkbox_nr'];
        
        echo 'Field Name '.($i).' <input name="field_name' . $i . '" value="' .  $field_name[$i][0] . '" />';       
      }

update_post_meta($post->ID, "field_name1", $_POST["field_name1"]);