Weird custom fields data lost

Hello guys,

I'm experiencing a weird issue with my wordpress. I'm around this for almost two months. Can someone please help me? Please be my life savior...

The issue:

When I publish a post with data in all fields (original and custom) everything seems ok. But after a few days (3-7 days on average), the data from my custom fields (only) disappear and I don't understand why. I already did the test of disabling all my plugins and I still have the same problem. So probably the issue is in my theme functions.php code, am I right?

Is there any kind soul that have enough time to check my code to see if there is anything that can cause this to happen, please?
My functions.php code: https://paste2.org/VvzxAdwA

The custom fields that I have and where the data disappears after a few days are:

---------------------------------------
mr_magnifying_glass

mr_random_post
mr_feature_video

today_offer

site_id1
site_id2
site_id3
site_id4
site_id5
site_id6
site_id7
site_id8

mr_active_discount
mr_topdeal_discount
mr_new_discount
mr_lifetime_discount
mr_discount_des
mr_discount_Image
mr_discountbutton_url

mr_quility_des
mr_content_des

mr_month_price
mr_month_off
mr_month_lastrice
mr_month_price_2
mr_sixmonth_price
mr_sixmonth_off
mr_sixmonth_lastrice
mr_per_month3
mr_threeyear_price
mr_threeyear_off
mr_threeyear_lastrice
mr_per_month6
mr_fiveyear_price
mr_fiveyear_off
mr_fiveyear_lastrice
mr_per_month12

mr_feature1
mr_feature2
mr_feature3
mr_feature4
mr_feature5
mr_feature6

catag
domaintag
post_external_link
joink_external_link
titletag

discount-extra-site[]
---------------------------------------

I don't know if this helps...

Please help me, I don't know what else to do...

Thank you, guys!

IMPORTANT: Please don't update any of my plugins.

Regards,
Tim

  • Nithin
    • Support Wizard

    Hi Tim,

    It's tough to state by just going through the given code to say where the issue is. Do you notice any debug log errors when this occurs? Also, does the issue occur with any specific custom post type, or with default post type?

    It seems like support access to your website is not enabled at the moment. Could you please grant support staff access so that we could have a better idea too about the workflow.

    You can grant access from WPMU DEV > Support > Support Access > Grant Access, or check this manual: https://premium.wpmudev.org/docs/getting-started/getting-support/#chapter-5

    Please let us know once you enable access so that we could guide you further. Have a nice weekend. :slight_smile:

    Kind Regards,
    Nithin

  • Tim
    • New Recruit

    Hi Nithin,

    Thank you very much for your response and for trying to help me.

    That's a good idea using the debug log. I checked my config and I already have enabled.
    This is what I have: https://paste2.org/vsxnUmaI

    Do you see anything suspicious? I don't remember the date that the data disappeared from the fields last time, but I'm going to check this every day.

    The issue only happens with the default post type.

    How about this? Do you think it can help? How to use it?
    define(‘SAVEQUERIES’, true);

    Thanks!!

    Kind regards,
    Tim

  • Nithin
    • Support Wizard

    Hi Tim,

    Sorry for the delay in getting back to you. From your given details most probably such issues could happen if save_post is called from another plugin or AJAX requests.

    It would be ideal to check the fields before updating, that's for example, instead of:
    update_post_meta($post->ID, 'mr_active_discount', $_POST['mr_active_discount']);

    The above could be better handled like this:

    if( isset( $_POST['mr_active_discount']) ) ){
     update_post_meta($post->ID, 'mr_active_discount', $_POST['mr_active_discount']);
    }

    Also, there are cURL timeout errors in the debug log, which you can fix by following the steps in the
    given link:
    https://docs.presscustomizr.com/article/326-how-to-fix-a-curl-error-28-connection-timed-out-in-wordpress

    How about this? Do you think it can help? How to use it?
    define(‘SAVEQUERIES’, true);

    Please check this link for more info:
    https://wordpress.stackexchange.com/questions/26491/view-queries-made

    Please do check, and see whether it helps. I hope this brings more clarity. Have a nice day ahead. :slight_smile:

    Regards,
    Nithin

  • Tim
    • New Recruit

    Hey Nithin,

    Thank you so much for your help.

    I already did the modification that you said, let's wait to see if the data from the fields disappear again or not. It is just this line to change, correct?

    From:

    if($post->post_type == 'post'){
    update_post_meta($post->ID, 'mr_active_discount', $_POST['mr_active_discount']);
    }

    To:

    if(($post->post_type == 'post')&&(isset($_POST['mr_active_discount']))){
    update_post_meta($post->ID, 'mr_active_discount', $_POST['mr_active_discount']);
    }

    What does this do? I don't use the field "mr_active_discount" in all the posts. It is important? But I use for example the field "post_external_link" in all of them.

    Btw, before I change the code, I notice that the fields were empty again.
    In my debug_log file, I've found these lines: https://paste2.org/4Ph9UhUz

    Those errors are in the same fields where data disappeared.
    Do you think that your suggestion could fix this for good?

    functions.php file updated: https://paste2.org/9MOHL2jt

    Thanks!

    Regards,
    Tim

  • Nithin
    • Support Wizard

    Hi Tim,

    What does this do? I don't use the field "mr_active_discount" in all the posts. It is important? But I use for example the field "post_external_link" in all of them.

    The code given was more of an example. You'll have to make sure to add an if condition for every update_post_meta function used in your code, so that it doesn't get invoked, or rewritten incorrectly.

    Those errors are in the same fields where data disappeared.
    Do you think that your suggestion could fix this for good?

    It should help, the isset function in the example code should make sure such logs aren't generated.

    Please do give a try, and see how it goes. Have a good day ahead. :slight_smile:

    Regards,
    Nithin

  • Tim
    • New Recruit

    Hello Nithin,

    Thank you once again for your help.
    I only did the changes that I posted above and it seems to work fine, I never had empty fields again :slight_smile:

    But today I have faced a new problem.
    This is a new project, and I was adding new posts on it.
    I created the first two posts without any problem, but I created three more posts and I filled the custom fields, but when I save as draft, no data was there... That's weird huh?
    Only saved the custom fields from first two posts, but in next new posts is not saving anymore.

    Any idea of what could be?

    functions.php file updated: https://paste2.org/9MOHL2jt

    Thanks!

    Regards,
    Tim

  • Tho Bui
    • Staff

    Hey there Tim,

    Regarding your issue, can you provide us the access credentials to site so w could do the checks?

    Note: Don't leave your login details in this ticket.
    Instead, you can send me your details using our contact form https://premium.wpmudev.org/contact/#i-have-a-different-question and the template below:

    Subject: "Attn: Tho Bui"
    - Site login URL
    - WordPress admin username
    - WordPress admin password
    - FTP/cPanel credentials (host/username/password)
    
    Kind Regards,
    Tho Bui
  • Tim
    • New Recruit

    Hey Tho Bui,

    Thank you for your response :slight_smile:

    Before I send you the access credentials... I think I solve the problem, but I don't know if the "fix" is good and If I'm going to have problems in the future... but is now working fine.

    I changed from:

    if(($post->post_type == 'post')&&(isset($_POST['mr_active_discount']))){
    update_post_meta($post->ID, 'mr_active_discount', $_POST['mr_active_discount']);

    To:

    if(($post->post_type == 'post')&&(isset($_POST['post_external_link']))){
    update_post_meta($post->ID, 'post_external_link', $_POST['post_external_link']);

    Basically, I only changed the custom field name from "mr_active_discount" to "post_external_link".

    What do you think?
    functions.php file updated: https://paste2.org/9MOHL2jt

    Regards,
    Tim

  • Tho Bui
    • Staff

    Hi Tim,

    Nice to hear about this!

    Sorry I don't have a change to see how are these fields on your post. But make sure all fields are same for all case.
    And one notice, the checkbox or radio field maybe don't exist why you submit if we without checked on them. So inside the function I suggest you check exist on checkbox/radio field too.
    And also if the field have disabled or readonly attribute you can't recieve the data too. So if you are not sure how about these, in the safe way, please check exist on EACH FIELD before you get the value to save.
    Ex:

    if( $post->post_type = 'post' && isset( $_POST['text_field_name_available_in_all_case_here'] ) ){
    	//your code here
    	if( isset( $_POST['field1'] ) ) {
    		// save field 1 here
    	}
    
    	if( isset( $_POST['field2'] ) ) {
    		// save field 1 here
    	}
    
    	// ....
    }

    And to arrange your code you can do like this:

    add_action('save_post_post', 'call_save_post_field_here', 10, 2);
    function call_save_post_field_here( $post_id, $post ){
    	if( isset( $_POST['text_field_name_available_in_all_case_here'] ) ){
    		//your code here
    		if( isset( $_POST['field1'] ) ) {
    			// save field 1 here
    		}
    
    		if( isset( $_POST['field2'] ) ) {
    			// save field 1 here
    		}
    		// ....
    	}
    }
    
    add_action('save_post_model', 'call_save_model_field_here', 10, 2);
    function call_save_post_field_here( $post_id, $post ){
    	if( isset( $_POST['text_field_name_available_in_all_case_here'] ) ){
    		//your code here
    		if( isset( $_POST['field1'] ) ) {
    			// save field 1 here
    		}
    
    		if( isset( $_POST['field2'] ) ) {
    			// save field 1 here
    		}
    		// ....
    	}
    }
    // ....

    Hope this help!

    Kind Regards,
    Tho Bui

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.