Skip Navigation

[Resolved] How to set format date field?

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

Problem:
How to create custom post title using custom field values with CRED forms.

Solution:
CRED API offers you hook "cred_save_data" to accoplish this goal. Also, custom fields values stored in postmeta table. You can get the custom field value using get_post_meta() function.

You can find proposed solution with the following reply:
https://toolset.com/forums/topic/how-to-set-format-date-field/#post-408928

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/cred-api/#csd

This support ticket is created 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Author
Posts
#408347

I'm writing according to:
https://toolset.com/forums/topic/how-to-set-the-content-of-the-post_title-field-from-custom-fields

Now it's all ok but I want to change date format. Now it's ten-digit number, the desired result is Y-m-d H:i

#408426

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Termo,

Thank you for contacting our support forum.

I'm assuming that you are making reference to the display on the frontend for the date.

What you can do is to have a look at our documentation below for how formatting works with the date fields

https://toolset.com/documentation/customizing-sites-using-php/functions/#date

Please let me know if this helps.

Thanks,
Shane

#408428

No, in previous thread I've got some code. One of custom fields is data and it isn't transmiting to post title in correct format. I can see ten-digits numer only. How to put date in format "Y-m-d H:i" ?

#408460

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Termo,

I know what you are referring to now 🙂

Your date is in Unix Timestamp format.

Could you send me the code so I can have a look at it ?

Thanks,
Shane

#408493

Code is in thread that I linked.

add_action('cred_save_data','func_custom_post_title',10,2);
 function func_custom_post_title($post_id,$form_data) {
    $type = get_post_type($post_id);
    if ($form_data['id']==9999)
    {
        $c1 = get_post_meta($post_id, 'wpcf-custom-field-1', true);
        $c2 = get_post_meta($post_id, 'wpcf-custom-field-2', true);
       $c3 = get_post_meta($post_id, 'wpcf-custom-field-3', true);
 
       $title= $c1. '-' . $c2. '-' .$c3;
 
        $slug = sanitize_title($title);
 
 
           $args = array('ID' => $post_id, 
                                   'post_title' => $title, 
                                   'post_name' => $slug
                          );
 
        wp_update_post($args);
    }
}
#408888

For example "wpcf-custom-field-1" is date field. How to display it in data formatting?

#408910

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Shane is on vacation. This is Minesh here to help you further and I'll take care of this ticket. Hope this is OK.

As Shane explain to you that Types date fields stores date as a timestap to database. You should use types shortcode to display the date at front-end. You do not need to change anything while you save date field.

So, to display the date field with format, please check following example:

[types field="my-date-field" format="Y/m/d g:i:s A"][/types]

More info:
https://toolset.com/documentation/customizing-sites-using-php/functions/#date

#408919

I need to change date format before transfer it to title.

#408928

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Considering "wpcf-custom-field-1" is date field.

You should try to use following code:

add_action('cred_save_data','func_custom_post_title',10,2);
 function func_custom_post_title($post_id,$form_data) {
    $type = get_post_type($post_id);
    if ($form_data['id']==9999)
    {
        $c1 = get_post_meta($post_id, 'wpcf-custom-field-1', true);
        $c1 = date("Y-m-d H:i",$c1);
        $c2 = get_post_meta($post_id, 'wpcf-custom-field-2', true);
       $c3 = get_post_meta($post_id, 'wpcf-custom-field-3', true);
  
       $title= $c1. '-' . $c2. '-' .$c3;
  
        $slug = sanitize_title($title);
  
  
           $args = array('ID' => $post_id, 
                                   'post_title' => $title, 
                                   'post_name' => $slug
                          );
  
        wp_update_post($args);
    }
}

I hope above code will help you to resolve your issue.

#408933

Thanks, problem solved.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.