Hello 🙂
I am building a sort of classified site via CRED front-end forms.
I managed to set expiration date of a post created by the front-end form - ([cred_generic_field field='expiration-date' type='date' class='' urlparam='']{"required":1,"validate_format":0,"default":""}[/cred_generic_field]).
Now I want to give users possibility to edit already created post and especially to edit the expiration date. So far I have been using the same code in the edit form as I used in the post creation form (stated above). Which (understandably) sets the new expiration date but does not load the initial value of the generic field "expiration-date". So the user has to set the post expiration date every time he/she opens the edit form. Which is, let's say, quite uncomfortable 🙂
Is there any way to load the already set expiration date into the edit form as a default value of some field? And store it back to post meta if the user either changes or either does not change the value of this field?
Thanks very much in advance,
Tom
Hello,
I suggest you try this:
1) You can get the custom field "expiration-date" value with Views shortcode [wpv-post-field name='expiration-date'],
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-field
2) then pass the value to generic field shortcode, like this:
[cred_generic_field field='expiration-date' type='date' class='' urlparam='']
{"required":1,"validate_format":0,"default":"[wpv-post-field name='expiration-date']"}
[/cred_generic_field]
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field
Hello,
thanks very much for reply.
I understand the Step 2, but I still do not understand the Step 1 - how do I assign the value to the custom field you mentioned?
As I have some 300+ posts created via CRED generic field mentioned in my original question I would love to avoid adding new custom field to some Post Field Group field and edit all the post already created.
I managed to get the expiration date value and display it in the form via [cred-post-expiration format='Y-m-d'] shortcode. Would it be perhaps possible to edit this shortcode to pass the value to the CRED generic field?
Thanks very much
Yes, it is possible to use generic field to edit post expiration date, Toolset form plugin is using a hidden custom field "_cred_post_expiration_time" to store the post expiration date value, so you can get that field value in timestamp format like this:
[wpv-post-field name='_cred_post_expiration_time']
Then use it in generic field shortcode, like this:
[cred_generic_field field='_cred_post_expiration_time' type='date' class='' urlparam='']
{"required":1,"validate_format":0,"default":"[wpv-post-field name='_cred_post_expiration_time']"}
[/cred_generic_field]
Notice, you will need to change the field attribute to "_cred_post_expiration_time" too.
Cool, we are getting close 🙂
This loads the expiration date (set via create post form) into the edit form but it does not save the loaded/user-changed value of this field to the post. So after saving the edit form the post has no expiration date...
Could you help again, please?
You are right, the generic field does not save the value into database by default, but you can use parameter "persist":1 in the shortcode, and save the data, for example:
[cred_generic_field field='_cred_post_expiration_time' type='date' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":"[wpv-post-field name='_cred_post_expiration_time']",
"persist":1
}
[/cred_generic_field]
Ok, this was not working either. But then I created action in my functions.php and it works.
Thanks for the help, my case is resolved now.
add_action('cred_save_data', 'set_expiry_dateb', 10, 2);
function set_expiry_dateb($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==139)
{
if (isset($_POST['_cred_post_expiration_time']['datepicker']))
{
// add it to saved post meta
update_post_meta($post_id, '_cred_post_expiration_time', $_POST['_cred_post_expiration_time']['datepicker']);
}
}
}
In case anyone interested, the solution is combination of this:
In the edit post form:
[cred_generic_field field='_cred_post_expiration_time' type='date' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":"[wpv-post-field name='_cred_post_expiration_time']",
"persist":1
}
[/cred_generic_field]
In the functions.php:
add_action('cred_save_data', 'set_expiry_dateb', 10, 2);
function set_expiry_dateb($post_id, $form_data)
{
// if a specific form - edit the number according to your edit post form ID
if ($form_data['id']==139)
{
if (isset($_POST['_cred_post_expiration_time']['datepicker']))
{
// add it to saved post meta
update_post_meta($post_id, '_cred_post_expiration_time', $_POST['_cred_post_expiration_time']['datepicker']);
}
}
}
Thanks for sharing the solution, that will help other users.
Strangely, I have tested the "persist" parameter in my localhost, it works just fine too.