Skip Navigation

[Resolved] Custom post type, form field blank after first use.

This thread is resolved. Here is a description of the problem and solution.

Problem:

Update single instance custom field value in PHP codes.

Solution:

Please try WP function update_post_meta(), for example:

https://toolset.com/forums/topic/custom-post-type-form-field-blank-after-first-use/#post-2027067

Relevant Documentation:

https://developer.wordpress.org/reference/functions/update_post_meta/

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 11 replies, has 2 voices.

Last updated by ianG-5 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2026701

Tell us what you are trying to do?

I have several custom post types with lots of different new & edit post forms, all seem to be working except one, the new post form seems to work fine, and the edit post works first time, but after the first save, one off the fields always displays blank even though there is still data saved (visible in wordpress back end). If you save the form again when the field is blank it saves as blank, if you put data in the blank field it will save that data.
I have this same setup on multiple forms / post types, the initial form has generic single line, then I have custom code to save that to the correct fields
add_post_meta( $post_id, 'wpcf-comm-diary-entry-org', $_POST['wpcf-comm-diary-entry'] );
add_post_meta( $post_id, 'wpcf-comm-diary-entry', $_POST['wpcf-comm-diary-entry'] );

and then in the edit form I am using(I have re added this from the menu to make sure there is no typos):
[cred_field field="comm-diary-entry" force_type="field" class="form-control" output="bootstrap"]

#2027067

Hello,

I assume we are talking about a single instance field, if it is, you just need to update the custom field value, please try WP function update_post_meta(), for example:

update_post_meta( $post_id, 'wpcf-comm-diary-entry', $_POST['wpcf-comm-diary-entry'] );

More help:
https://developer.wordpress.org/reference/functions/update_post_meta/

#2027171

The form updates the fields, the issue is that the form does not show what is in the database (or WP admin)
The database has content but the field is loaded as empty.

I think I may have confused you with the

add_post_meta( $post_id, 'wpcf-comm-diary-entry-org', $_POST['wpcf-comm-diary-entry'] );

This is what I use after another form to save a single Single Line to two WYSIWYG fields.
The initial add works fine, and the first edit loads and saves fine, the second time the edit form is loaded it shows blank fields despite there being data in the db

#2027197

It is a custom codes problem, please provide a test site with the same problem, also point out the problem page URL, form URL and where I can edit your PHP codes, I need a live website to test and debug, thanks

#2029311

Thanks for the details, I can login into your website, the problem is you are using add_post_meta() function, and save duplicated values into the same custom field "wpcf-comm-diary-entry-org".

I have done below modifications in your website:
Edit the snippet code "comm-diary-save-data", change it from:

add_post_meta( $post_id, 'wpcf-comm-diary-entry', $_POST['wpcf-comm-diary-entry'] );
         delete_post_meta( $post_id, 'wpcf-comm-diary-entry' );
         update_post_meta( $post_id, 'wpcf-comm-diary-entry', $_POST['wpcf-comm-diary-entry'] );

Please test again, check if it is fixed.

Please let me know if you need assistance to apply same change for another field "wpcf-comm-diary-entry-org"

#2029359

Thanks Luo, but I am confused by this fix.
Can you please explain to me why this was leading to a blank from in the front end but still showing data in the admin panel?
Was it creating multiple instances of the meta or something?

On that same note, why is delete meta needed before update? I don't believe this is normal?

#2029373

Let me explain it again:
Since you was using add_post_meta() function to add new instance into custom field "wpcf-comm-diary-entry", there will be multiple duplicated instances in the custom field "wpcf-comm-diary-entry", and conducts the problem: leading to a blank from in the front end but still showing data in the admin panel

In order to remove those multiple duplicated instances of custom field "wpcf-comm-diary-entry", you have to use function delete_post_meta() to delete them, then use update_post_meta() function to update the custom field value.

Hope it is clear.

#2029425

My issue is resolved now. Thank you!

#2029475

Correction, sorry, I am still confused the form im using is ID 3126,

function set_diary_content($post_id, $form_data){
	//check to make sure we are using cred form with correct ID 
    if ($form_data['id'] == 1123 || 4647) { 
       //if the Post variable is set
       if ( isset($_POST['wpcf-comm-diary-entry']) ){
         //set 'original-ad' to 'current-ad' field.
         add_post_meta( $post_id, 'wpcf-comm-diary-entry-org', $_POST['wpcf-comm-diary-entry'] );
         add_post_meta( $post_id, 'wpcf-comm-diary-entry', $_POST['wpcf-comm-diary-entry'] );
       }
    }
}

Surely this function should not be running?

#2029477
$form_data['id'] == 1123 || 4647

Should be

$form_data['id'] == 1123 || form_data['id'] ==4647

still find it odd and concerning that php passed the condition.

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/i-am-still-confused-the-form-ids-in-php-codes/

#2030501

I assume the original question of this thread is resolved, for the other new questions, please check the new thread here:
https://toolset.com/forums/topic/i-am-still-confused-the-form-ids-in-php-codes/

#2030813

Thank you!