• I’m dealing with the most baffling problem and I’m at my wits end. I’m hoping someone else might have an idea what the problem is! I have a custom post type called “Products” with about 600 posts. There are several custom fields/inputs for additional info about the products. There is a single product among those 600 that keeps going blank – just the one. All of the custom field meta data gets wiped out for no apparent reason.

    I have the code set to check for autosaving to prevent the erasing of data, but because this has been a persistent problem I disabled autosaving all-together. Still the product goes blank.

    We’ve even tried deleting the product and creating it anew but the new one gets wiped out.

    I don’t have the foggiest idea what else to look at or check. Has anyone else ever seen something like this happen? Can someone point me in the right direction?

    Thanks!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter Jess

    (@jessn)

    This issue is persisting. Does anyone know where I could look to fix the problem?

    I would need to see the function you use with add_action(‘save_post’, to understand how you are saving it (or not)

    If you could pastebin it please

    Thread Starter Jess

    (@jessn)

    Thank you so much. Here’s what I have:

    https://pastebin.com/32qvEcaG

    … you’re not saving any custom meta fields .. at all, according to that … where’s your add_post_meta, update_post_meta and delete_post_meta ?

    Thread Starter Jess

    (@jessn)

    Sorry, didn’t think you needed that part. I’ve added a bit more:

    https://pastebin.com/SzNknJFE

    k, try this instead

    function save_details($post_id, $post) {
    	global $post;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    		return $post->ID;
    	}
    
    	/* Get the post type object. */
    	$post_type = get_post_type_object( $post->post_type );
    
    	/* Check if the current user has permission to edit the post. */
    	if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
    		return $post_id;
    
    	$meta_array = array(
    			'featured_title',
    			'featured_img'
    			);
    
    	foreach ($meta_array as $meta_key) {
    		$new_meta_value = ( isset( $_POST[$meta_key] ) ? esc_textarea( $_POST[$meta_key] ) : '' );
    		$meta_value = get_post_meta( $post_id, $meta_key, true );
    		if ( $new_meta_value && '' == $meta_value )
    			add_post_meta( $post_id, $meta_key, $new_meta_value, true );
    		elseif ( $new_meta_value && $new_meta_value != $meta_value )
    			update_post_meta( $post_id, $meta_key, $new_meta_value );
    		elseif ( '' == $new_meta_value && $meta_value )
    			delete_post_meta( $post_id, $meta_key, $meta_value );
    	}
    }

    One of the things I did notice is you’re not passing the $post_id, or $post inside the function call, so it was dependent on the global – with all of your other meta fields that you didn’t show you can add those to the $meta_array each on a new string

    Thread Starter Jess

    (@jessn)

    Oh I love how you wrote that – that’s a much cleaner way of doing it. However I just placed the code in and the meta fields don’t save. I hit “update” and the fields just go blank

    Then verify the exact spelling of the featured_img and featured_title in your other part of the code, some people put featured-img and featured-title accidently switching the underscore and dash; I believe you’re actually supposed to use dash instead of underscore ;/

    Yeah, use dashs instead of underscore on those names

    Thread Starter Jess

    (@jessn)

    Thanks so much for your help Frumph. It’s still not saving the data, but I’ll keep tinkering with it

    Thread Starter Jess

    (@jessn)

    Oh and thank you for mentioning the dash vs underscore issue. I always use underscores, but I double checked my spelling, and then tried changing things to dashes. Nada. But I like the way you did things above – I’ll keep working on it because I’d like to make that method work

    this is how I do the admin post meta stuff for custom post types:

    https://github.com/Frumph/comic-easel/blob/master/functions/admin-meta.php

    if you want to use as a reference

    it could very well be because your add_action isn’t in the action for admin_init for all I can remember at the moment ;/

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Single post in CPT being erased’ is closed to new replies.