• Regis

    (@regiswordpress)


    I have a custom post type for which I use a few custom fields to save additional data. Everything works perfectly until I save my post in the Quick Edit editor. Then all custom meta data is deleted? any ideas?
    The only difference in my code I see with the Codex example is that I don’t return the saved data at the end of the saving function… is that necessary? What does it do?
    I would not know what to return as I update multiple keys with multiple calls to update_post_meta…

    function save_options(){
    
    global $post;
    
    if ( !wp_verify_nonce( $_POST['myNonceName'], 'myThemeName' ))
    {
         return $post_id;
    }
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    {
         return $post_id;
    }
    
    update_post_meta($post->ID, "textsubtitle", $_POST["textsubtitle"]);
    update_post_meta($post->ID, "textauthor", $_POST["textauthor"]);
    update_post_meta($post->ID, "textshowchildren", $_POST["textshowchildren"]);
    update_post_meta($post->ID, "textforceprocess", $_POST["textforceprocess"]);
    update_post_meta($post->ID, "textclearformat", $_POST["textclearformat"]);
    update_post_meta($post->ID, "subtext", $_POST["subtext"]);
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Regis

    (@regiswordpress)

    Also, when I save data in the ‘Edit’ section, if deletes the data I saved in the ‘Quick Edit’ section…. like page hierarchy…

    Maybe the solution here will help..
    https://core.trac.www.ads-software.com/ticket/13197

    And another similar ticket.
    https://core.trac.www.ads-software.com/ticket/14192

    Googled with the following (incase you’re wondering how i found the information).
    site:https://core.trac.www.ads-software.com custom post type quick edit

    Thread Starter Regis

    (@regiswordpress)

    Thank you Marc. I found out was I was doing wrong — my nonce was badly named. But in the meantime I discovered something else. After setting a page hierarchy in the quick edit mode, I would loose the info when saving in the full edit mode. I’d created a custom post type with the option ‘hierarchical’ set to ‘true’, but I had not added support for ‘page-attributes’. So I guess that when I updated my post from the full edit mode, the hierarchy info was overwritten as empty… here is my updated code, working like a charm:

    // the custom post type declaration:
    $custom_args = array(
    	'labels' => array(
    	'name' => __( 'Things' ),
    	'singular_name' => __( 'Thing' ),
    	'add_new' => _x('Add New', 'Thing'),
    	'add_new_item' => __('Add New Thing'),
    	'edit_item' => __('Edit Thing'),
    	'new_item' => __('New Thing'),
    	'view_item' => __('View Thing'),
    	'search_items' => __('Search Things'),
    	'not_found' =>  __('No Things found'),
    	'not_found_in_trash' => __('No Things found in Trash'),
            'parent_item_colon' => ''
    	),
       'public' => true,
       'publicly_queryable' => true,
       'show_ui' => true,
       'query_var' => true,
       'rewrite' => true,
       'capability_type' => 'post',
       'hierarchical' => true,
       'menu_position' => 9,
       'supports' =>array('title','editor','author','thumbnail','excerpt','comments', 'page-attributes') );
    register_post_type ('things', $custom_args );

    I have also added a function to sanitize the input from the user.

    function save_options(){
    [...]
    update_post_meta($post->ID, "textsubtitle",cleanInput( $_POST["textsubtitle"], 'text') );
    update_post_meta($post->ID, "textauthor", cleanInput($_POST["textauthor"],'text') );
    update_post_meta($post->ID, "textshowchildren", cleanInput( $_POST["textshowchildren"], 'checkbox') );
    update_post_meta($post->ID, "textforceprocess", cleanInput( $_POST["textforceprocess"], 'checkbox') );
    update_post_meta($post->ID, "textclearformat", cleanInput( $_POST["textclearformat"], 'checkbox') );
    update_post_meta($post->ID, "subtexttitle", cleanInput( $_POST["subtexttitle"], 'text') );
    update_post_meta($post->ID, "subtextauthor", cleanInput( $_POST["subtextauthor"],'text') );</p>
    <p>return $_POST; //I am still not sure why or if I have to
                   // return anything here...
    }

    and finally, my very simple function to verify input:

    function cleanInput( $input, $type ) {
    	global $allowedposttags;
    	$cleanInput = false;
    	switch ($type) {
    	  case 'text':
    	    $cleanInput = wp_filter_nohtml_kses ( $input );
    	    break;
              case 'checkbox':
                $input === 'on'? $cleanInput = 'on' : $cleanInput = '';
    	    break;
              case 'html':
                $cleanInput = wp_kses( $input, $allowedposttags);
    	    break;
    	default:
    	    $cleanInput = false;
    	    break;
    	}
    	return $cleanInput;
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Post meta in Custom post type, deleted by quick edit…’ is closed to new replies.