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"]
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/
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
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
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"
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?
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.
My issue is resolved now. Thank you!
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?
$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.
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/