Search code examples
phploopsmetadatacustom-post-type

How to Loop thru Custom Meta Box values for Custom Post Type


The functions below set up a meta box for a custom post type and check / save the values in a meta box for the custom post type.

However, there are 25 of these custom "error code names" and also 25 "error code descriptions". Instead of duplicating these functions 25x for each custom meta I would like to create a loop that does the same thing as below because once I start utilizing this post-type I may run across the need to further increase the amount of meta error code boxes but I am not sure how to go about doing this.

Change this to where it loops through save_error_code_name_01 - save_error_code_name_25 and save_error_code_desc_01 - save_error_code_desc_25.

function error_codes_setup() {
   add_action( 'add_meta_boxes', 'add_error_code_box' );
   add_action( 'save_post', 'save_error_code_name_XX', 10, 2 );
   ... repeat 25x ...
   add_action( 'save_post', 'save_error_code_desc_XX', 10, 2 );
   ... repeat 25x ...
}

function add_error_code_box(){
   add_meta_box('error_code_box', esc_html__('Error Codes', 'example'),'error_code_box',
   'error_codes', 'normal', 'low');
}

Change this to where it generates new input types for error_code_name and title 25x each.

function error_code_box ( $post ){
   wp_nonce_field( basename( __FILE__ ), 'error_codes_nonce' ); ?>
   <table>
      <tr><th><label>Error Code Name</label></th>
      <th><label>Error Code Description</label></th></tr>
      <tr>
         <td><input type="text" name="error_code_name" id="error_code_name" class="error_code_name"
         value="<?php echo esc_attr(get_post_meta($post->ID, 'error_code_name', true));?>" size="15"/></td>
          ... Repeat 25x ....
         <td><input type="text" name="error_code_desc" id="error_code_desc" class="error_code_desc" 
         value="<?php echo esc_attr(get_post_meta($post->ID, 'error_code_desc', true));?>" size="60"/></td>
          ... Repeat 25x ....
      </tr>
   </table>

Change this to where it loops thru every custom_meta to check its status and save or not.

function save_error_code_name( $post_id, $post ) {
if ( !isset( $_POST['error_codes_nonce'] ) || !wp_verify_nonce( $_POST['error_codes_nonce'], basename( __FILE__ ) ) )
    return $post_id;

$post_type = get_post_type_object( $post->post_type );
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
    return $post_id;

$new_value = ( isset( $_POST['error_code_name'] ) ? ( $_POST['error_code_name'] ) : ’ );
$meta_key  = 'error_code_name';
$value     = get_post_meta( $post_id, $meta_key, true );
if ($new_value && ’ == $meta_key )
    add_post_meta( $post_id, $meta_key, $value, true );
if ( $new_value && ’ == $value )
    add_post_meta( $post_id, $meta_key, $new_value, true );
elseif ( $new_value && $new_value != $value )
    update_post_meta( $post_id, $meta_key, $new_value );  
elseif ( ’ == $new_value && $value )
    delete_post_meta( $post_id, $meta_key, $value );        
}

Solution

  • Found a working solution. But upon implementation got a series of undefined constant warnings. Ended up having to replace the reverse single quotation marks with null in order to get it working correctly.

    function error_code_box ( $post ){
    wp_nonce_field( basename( __FILE__ ), 'error_codes_nonce' ); ?>
    <table><tr><th><label>Error Code Name</label></th>
        <th><label>Error Code Description</label></th></tr><?php 
        $x = 0;
        $y = 0;     
        for ($i = 0; $i < 20; $i++){ ?>
        <tr>
            <td>
                <input type="text" name= "error_code_name_<?php echo ++$x;?>" id="error_code_name_<?php echo $x;?>" class="error_code_name" value="<?php echo esc_attr(get_post_meta($post->ID, 'error_code_name_'.$x.'', true));?>" size="15" />
            </td>
            <td>
                <input type="text" name= "error_code_desc_<?php echo ++$y;?>" id="error_code_desc_<?php echo $y;?>" class="error_code_desc" value="<?php echo esc_attr(get_post_meta($post->ID, 'error_code_desc_'.$y.'', true));?>" size="60" />
            </td>       
        </tr>
        <?php } ?>
    </table><?PHP 
    } 
        
    function save_error_code_name( $post_id, $post ) {
       $x = 0;  
       for ($i = 0; $i < 20; $i++){ 
           if ( !isset( $_POST['error_codes_nonce'] ) || !wp_verify_nonce( $_POST['error_codes_nonce'], basename( __FILE__ ) ) )
              return $post_id;
              $post_type = get_post_type_object( $post->post_type );
           if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
              return $post_id;
        
           $new_value = ( isset( $_POST['error_code_name_'.++$x.''] ) ? ( $_POST['error_code_name_'.$x.''] ) : null );
           $meta_key  = 'error_code_name_'.$x.'';
           $value     = get_post_meta( $post_id, $meta_key, true );
           if ($new_value && null == $meta_key )
               add_post_meta( $post_id, $meta_key, $value, true );
           if ( $new_value && null == $value )
              add_post_meta( $post_id, $meta_key, $new_value, true );
           elseif ( $new_value && $new_value != $value )
              update_post_meta( $post_id, $meta_key, $new_value );  
           elseif ( null == $new_value && $value )
              delete_post_meta( $post_id, $meta_key, $value );
        }
     }