• Resolved ryHollingsworth

    (@ryhollingsworth)


    Hoping someone can easily make me feel stupid because I’m banging my head on something probably really small that I’ve over looked here. I’ve created a custom post type, which also creates some categories when the theme is activated- all that is good. I also am creating several custom meta boxes, which is where the problem lies. If input data into the meta boxes and hit save/update/publish everything is fine. If i come back, and hit update in the future the meta boxes that I did not put in new data are lost. (only the newly updated meta boxes are saved and it deletes the others)

    Here’s the entire post type:

    <?
    
    //register post type
    function register_resume() {
    	$labels = array(
    		'name'               => _x( 'Resume', 'post type general name' ),
    		'singular_name'      => _x( 'resume', 'post type singular name' ),
    		'add_new'            => _x( 'Add New', 'book' ),
    		'add_new_item'       => __( 'Add New Resume Item' ),
    		'edit_item'          => __( 'Edit Items' ),
    		'new_item'           => __( 'New Item' ),
    		'all_items'          => __( 'All Resume Items' ),
    		'view_item'          => __( 'View Resume Items' ),
    		'search_items'       => __( 'Search Resume Items' ),
    		'not_found'          => __( 'No items found' ),
    		'not_found_in_trash' => __( 'No items found in the Trash' ),
    		'parent_item_colon'  => '',
    		'menu_name'          => 'Resume'
    	);
    	$args = array(
    		'labels'        	   => $labels,
    		'description'   	   => 'Holds our resume and resume specific data',
    		'public'        	   => true,
    		'menu_position' 	   => 5,
    		'supports'      	   => array( 'title', 'editor'),
    		'has_archive'   	   => false,
    		'taxonomies'		   => array('Education','Work','Code','Software'),
    		'register_meta_box_cb' => 'declare_meta_boxes'
    
    	);
    	register_post_type( 'resume', $args );
    
    }
    add_action( 'init', 'register_resume' );
    
    //create taxonomy
    function taxonomy_resume() {
    	$labels = array(
    		'name'              => _x( 'Resume', 'taxonomy general name' ),
    		'singular_name'     => _x( 'Resume Category', 'taxonomy singular name' ),
    		'search_items'      => __( 'Search Resume Categories' ),
    		'all_items'         => __( 'All Resume Categories' ),
    		'parent_item'       => __( 'Parent Resume Category' ),
    		'parent_item_colon' => __( 'Parent Resume Category:' ),
    		'edit_item'         => __( 'Edit Resume Category' ),
    		'update_item'       => __( 'Update Resume Category' ),
    		'add_new_item'      => __( 'Add New Resume Category' ),
    		'new_item_name'     => __( 'New Resume Category' ),
    		'menu_name'         => __( 'Resume Categories' ),
    	);
    	$args = array(
    		'labels' => $labels,
    		'hierarchical' => true,
    	);
    	register_taxonomy( 'resume_category', 'resume', $args );
    
    }
    add_action( 'init', 'taxonomy_resume', 0 );
    
    function add_resume_categories() {
    	$categories = array("Education","Work","Code","Software","Skills", "Awards");
    
    	foreach($categories as $category):
    		wp_insert_term($category,'resume_category');
    	endforeach;
    
    }
    add_action( 'after_switch_theme', 'add_resume_categories' );
    
    function declare_meta_boxes($post){
    	declare_copy_text_meta_box($post);
    	declare_acommplishments_meta_box($post);
    }
    
    function save_meta_boxes($post){
    	copy_text_save_metabox($post);
    	acommplishments_save_metabox($post);
    }
    
    add_action('save_post', 'save_meta_boxes');
    
    function declare_copy_text_meta_box($post){
        add_meta_box(
        	'copy_text_meta_box',
        	'Degree or Job Title',
        	'copy_text_meta_box',
        	$post->post_type,
        	'normal' ,
        	'low'
        );
    }
    
    function copy_text_save_metabox(){
        global $post;
        if(isset($_POST["copy_text"])){
             //UPDATE:
            $meta_element_class = $_POST['copy_text'];
            //END OF UPDATE
    
            update_post_meta($post->ID, 'copy_text_meta_box', $meta_element_class);
            //print_r($_POST);
        }
    }
    
    function copy_text_meta_box($post){
        $meta_element_class = get_post_meta(
        	$post->ID,
        	'copy_text_meta_box',
        	true
        );
        ?>   
    
    <label>List Degree or Job Title:  </label>
    <input type="text" name="copy_text" id="copy_text"
    placeholder="<?=$meta_element_class?>"/>
    
        <?php
    }
    
    $accomplishment_total = 5;
    
    function declare_acommplishments_meta_box($post){
        add_meta_box(
        	'acommplishments_meta_box',
        	'Accomplishments',
        	'acommplishments_meta_box',
        	$post->post_type,
        	'normal' ,
        	'low'
        );
    }
    
    function acommplishments_save_metabox($accomplishment_total){ 
    
        global $accomplishment_total;
        global $post;
        $accomplishment_total ? $accomplishment_total : $accomplishment_total = 1;
    
    	for ($i=1; $i<=$accomplishment_total; $i++):
    
    	    if(isset($_POST["acommplishments$i"])):
    	         //UPDATE:
    	        $meta_element_class = $_POST["acommplishments$i"];
    	        //END OF UPDATE
    
    	        update_post_meta(
    	        	$post->ID,
    	        	"acommplishments_meta_box$i",
    	        	$meta_element_class);
    		endif;
        endfor;
    }
    
    function acommplishments_meta_box($post){
    
        global $accomplishment_total;
        $accomplishment_total ? $accomplishment_total : $accomplishment_total = 1;
    
        for ($i=1; $i<=$accomplishment_total; $i++):
    
    	    $meta_element_class = get_post_meta(
    	    	$post->ID,
    	    	"acommplishments_meta_box$i",
    	    	true
    	    ); 
    
        ?>   
    
    		<label>Accomplishment <?=$i?>:</label>
    		<input type="text" name="acommplishments<?=$i?>" id="acommplishments<?=$i?>"
    		placeholder="<?=$meta_element_class?>"/>
    		<div class="clear"></div>
    
        <?php
        endfor;
    }
Viewing 1 replies (of 1 total)
  • Thread Starter ryHollingsworth

    (@ryhollingsworth)

    Just deleted and started over my meta boxes with a fresh brain and solved my problem. Posting the resolution incase someone else has a similar issue

    <?
    
    //register post type
    function register_resume() {
    	$labels = array(
    		'name'               => _x( 'Resume', 'post type general name' ),
    		'singular_name'      => _x( 'resume', 'post type singular name' ),
    		'add_new'            => _x( 'Add New', 'book' ),
    		'add_new_item'       => __( 'Add New Resume Item' ),
    		'edit_item'          => __( 'Edit Items' ),
    		'new_item'           => __( 'New Item' ),
    		'all_items'          => __( 'All Resume Items' ),
    		'view_item'          => __( 'View Resume Items' ),
    		'search_items'       => __( 'Search Resume Items' ),
    		'not_found'          => __( 'No items found' ),
    		'not_found_in_trash' => __( 'No items found in the Trash' ),
    		'parent_item_colon'  => '',
    		'menu_name'          => 'Resume'
    	);
    	$args = array(
    		'labels'        	   => $labels,
    		'description'   	   => 'Holds our resume and resume specific data',
    		'public'        	   => true,
    		'menu_position' 	   => 5,
    		'supports'      	   => array( 'title', 'editor'),
    		'has_archive'   	   => false,
    		'taxonomies'		   => array('Education','Work','Code','Software'),
    		'register_meta_box_cb' => 'add_resume_metaboxes'
    
    	);
    	register_post_type( 'resume', $args );
    
    }
    add_action( 'init', 'register_resume' );
    
    //create taxonomy
    function taxonomy_resume() {
    	$labels = array(
    		'name'              => _x( 'Resume', 'taxonomy general name' ),
    		'singular_name'     => _x( 'Resume Category', 'taxonomy singular name' ),
    		'search_items'      => __( 'Search Resume Categories' ),
    		'all_items'         => __( 'All Resume Categories' ),
    		'parent_item'       => __( 'Parent Resume Category' ),
    		'parent_item_colon' => __( 'Parent Resume Category:' ),
    		'edit_item'         => __( 'Edit Resume Category' ),
    		'update_item'       => __( 'Update Resume Category' ),
    		'add_new_item'      => __( 'Add New Resume Category' ),
    		'new_item_name'     => __( 'New Resume Category' ),
    		'menu_name'         => __( 'Resume Categories' ),
    	);
    	$args = array(
    		'labels' => $labels,
    		'hierarchical' => true,
    	);
    	register_taxonomy( 'resume_category', 'resume', $args );
    
    }
    add_action( 'init', 'taxonomy_resume', 0 );
    
    function add_resume_categories() {
    	$categories = array("Education","Work","Code","Software","Skills", "Awards");
    
    	foreach($categories as $category):
    		wp_insert_term($category,'resume_category');
    	endforeach;
    
    }
    add_action( 'after_switch_theme', 'add_resume_categories' );
    
    //add_action( 'add_meta_boxes', 'add_resume_metaboxes' );
    
    // Add Meta Boxes
    function add_resume_metaboxes() {
    	global $post;
    	add_meta_box(
    		'resume_meta_boxes',
    		'Accomplishments',
    		'resume_add_meta_boxes',
    		 $post->post_type,
    		'normal',
    		'high'
    	);
    
    	add_meta_box(
    		'title_meta_boxes',
    		'Degree Or Job Title',
    		'title_add_meta_boxes',
    		 $post->post_type,
    		'normal',
    		'high'
    	);
    }
    
    $accomplishment_names 	= array();
    $ttl = 5;
    
    for ($i=1; $i<=$ttl; $i++):
    	array_push($accomplishment_names, "accomplishment_$i");
    endfor;
    
    // Create Metabox
    function resume_add_meta_boxes() {
    	global $post;
    	global $accomplishment_names;
    	echo '<input type="hidden" name="accompmeta_noncename" id="accompmeta_noncename" value="' .
    	wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    
    	$i=1;
    
    	foreach($accomplishment_names as $accomplishment):
    
    		$name = $accomplishment;
    		$value = get_post_meta($post->ID, $name, true);
    		$label = "Accomplishment $i";
    
    		$i++;
    
    	    echo "<p>$label:</p>";
    		echo "<input type='text' name='$name'value='$value' class='widefat'/>";
    	endforeach;
    }
    
    function resume_save_meta($post_id, $post) {
    
    	global $accomplishment_names;
    
    	array_push($accomplishment_names, 'title');
    	//this is the title meta box for degree/title
    	//adding it to the array so we don't have to do any more code
    
    	if ( !wp_verify_nonce( $_POST['accompmeta_noncename'], plugin_basename(__FILE__) )) {
    	return $post->ID;
    	}
    	if ( !current_user_can( 'edit_post', $post->ID ))
    		return $post->ID;
    
    		foreach ($accomplishment_names as $name):
    			$accomplishments[$name] = $_POST[$name];
    		endforeach;
    
    		foreach ($accomplishments as $key => $value) {
    			if( $post->post_type == 'revision' ) return;
    			$value = implode(',', (array)$value);
    			if(get_post_meta($post->ID, $key, FALSE)) {
    				update_post_meta($post->ID, $key, $value);
    			} else {
    				add_post_meta($post->ID, $key, $value);
    			}
    		if(!$value) delete_post_meta($post->ID, $key);
    	}
    }
    add_action('save_post', 'resume_save_meta', 1, 2); 
    
    function title_add_meta_boxes() {
    	global $post;
    
    	echo '<input type="hidden" name="titlemeta_noncename" id="titlemeta_noncename" value="' .
    	wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    
    	$name = 'title';
    	$value = get_post_meta($post->ID, $name, true);
    	$label = "Degree Or Job Title";
    
    	$i++;
    
        echo "<p>$label:</p>";
    	echo "<input type='text' name='$name'value='$value' class='widefat'/>";
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Custom Post Type Save/Update Malfunction’ is closed to new replies.