Skip Navigation

[Resolved] auto create post title

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 -
- - - - - - -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 9 replies, has 2 voices.

Last updated by Adriano 9 years ago.

Assisted by: Adriano.

Author
Posts
#288240

I need to auto create post title like this https://toolset.com/forums/topic/auto-create-post-title/ but I need to insert date field of current post and a text field of parent post. And how can I save this title if I use CRED form to create the post.
I tried to use this code:

add_filter ('title_save_pre','combine_title');
function combine_title($title) {
global $post;
$type = get_post_type($post->ID);
if ($type == 'employee') {
    $title = date('d.m.Y', strtotime(get_post_meta($post->ID, 'wpcf-date', true)));
    $parent = $post->post_parent;
    $title .= ', ' . get_post_meta($parent->ID, 'wpcf-name', true);
}
return $title;
}

It inserts noncorrect date, can't take parent post id and when I use CRED to make post, title isn't adding.

#288522

Hi Dinara,

You code looks good. Are custom field slugs right (wpcf-date, wpcf-name)? Is it a Child theme? What title is being saved?

#288832

It is not child theme.
The code I use exactly looks so:

add_filter ('title_save_pre','combine_title');
function combine_title($title) {
global $post;
$type = get_post_type($post->ID);
if ($type == 'vipusk') {
    $title = date('d.m.Y', strtotime(get_post_meta($post->ID, 'wpcf-data-vipuska', true)));
    $parent = $post->post_parent;
    $title .= ', ' . get_post_meta($parent->ID, 'wpcf-nazvanie', true);
}
return $title;
}

wpcf-data-vipuska - date custom field, which is used in post;
wpcf-nazvanie - text custom field, which is used in parent post.
I need to recieve title like "wpcf-nazvanie, wpcf-data-vipuska".
Now I recieve only date and it is not date from post. I recieve "12.03.4400" - date and month from today and 4400 year.

#289029

Hi Dinara,

Ok, first you don't need strtotime, because date fields are stored as timestamp already. So the code below is enough:

date('d.m.Y', get_post_meta($post->ID, 'wpcf-data-vipuska', true));

And use wp_get_post_parent_id in order to get the ID of the parent (it won't work for parent/post relationship established with Types):

$parent = wp_get_post_parent_id($post->ID);

If the relationship is established through Types, you can use the code below:

$parent = get_post_meta($post->ID, '_wpcf_belongs_PARENT-SLUG_id', true);

Replacing PARENT-SLUG with the right one.

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,

Adriano Ferreira

#289367

Thank you, Adriano.
Now it works if I make post through dashboard.
If I try to use CRED, it doesn't work and need to be updated for complete auto title. What I need to do for create auto title only by submitting CRED form (without any additional steps)?
And I have one more problem: when I enter very old date (for example, 1915 year) it says "This field is required".

#290591

Hi Dinara,

I'm still checking that CRED issues with the title. The date bug has been fixed already, I've sent a development version for you by email, please update yours and let me know your feedback.

#290994

Hi, Adriano!
Old dates now is ok, thank you!

#291368

Hi Dinara,

I'm glad to hear that. About changing he title on CRED form, you could use wp_update_post function:

https://codex.wordpress.org/Function_Reference/wp_update_post

With CRED API hook:

https://toolset.com/documentation/user-guides/cred-api/#csd

Something like that:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)
 
{

$title = date('d.m.Y', get_post_meta($post->ID, 'wpcf-data-vipuska', true));
    $parent = wp_get_post_parent_id($post->ID);
//$parent = get_post_meta($post->ID, '_wpcf_belongs_PARENT-SLUG_id', true);
    $title .= ', ' . get_post_meta($parent->ID, 'wpcf-nazvanie', true);

$my_post = array(
      'ID'           => $post_id,
      'post_title' => $title
  );

// Update the post into the database
  wp_update_post( $my_post );  
}
}

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,

Adriano Ferreira

#293496

Hi Adriano!
Sorry for my long silence. I had to work on another project last days.
Now I carefully tested auto post title and it works!
Thank you very much!

#293694

Glad to hear. You are welcome.

The forum ‘Types Community Support’ is closed to new topics and replies.

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