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.
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?
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.
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
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".
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.
Hi, Adriano!
Old dates now is ok, thank you!
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
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!
Glad to hear. You are welcome.