WordPress.org

Ready to get started?Download WordPress

Forums

Single post in CPT being erased (14 posts)

  1. jessn
    Member
    Posted 1 year ago #

    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!

  2. jessn
    Member
    Posted 1 year ago #

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

  3. Frumph
    Member
    Posted 1 year ago #

    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

  4. jessn
    Member
    Posted 1 year ago #

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

    http://pastebin.com/32qvEcaG

  5. Frumph
    Member
    Posted 1 year ago #

    ... 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 ?

  6. jessn
    Member
    Posted 1 year ago #

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

    http://pastebin.com/SzNknJFE

  7. Frumph
    Member
    Posted 1 year ago #

    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

  8. jessn
    Member
    Posted 1 year ago #

    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

  9. Frumph
    Member
    Posted 1 year ago #

    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 ;/

  10. Frumph
    Member
    Posted 1 year ago #

    Yeah, use dashs instead of underscore on those names

  11. jessn
    Member
    Posted 1 year ago #

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

  12. jessn
    Member
    Posted 1 year ago #

    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

  13. Frumph
    Member
    Posted 1 year ago #

    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

  14. Frumph
    Member
    Posted 1 year ago #

    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 ;/

Topic Closed

This topic has been closed to new replies.

About this Topic