Skip Navigation

[Resolved] Set expiration date as default value for date field

This support ticket is created 5 years, 10 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 8 replies, has 2 voices.

Last updated by Luo Yang 5 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1183761

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

#1184027

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

#1184033

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

#1184041

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.

#1184044

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?

#1184060

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]
#1184078

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']);
}
}
}

#1184080

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']);
}
}
}

#1184109

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.