Auto Generate Title for Custom Post / inherit parent page title
-
Hi,
I am trying to automatically generate a post title that I can use as a unique ID for my posts.
Each post should get a unique ID using a prefix ‘g’, ‘p’ and ‘c’ for grandparent, parent and child posts plus a timestamp. I have added the following code to my child theme functions.php
<?php add_filter('wp_insert_post_data','dsgnwrks_update_title',99,2); function dsgnwrks_update_title($data, $postarr) { global $post; if ( !is_admin() ) return $data; if ($data['post_type'] == 'my-post-type-name') { $data['post_name'] = sanitize_title( 'g-'.date_i18n( 'Y-m-j-G-i-s', strtotime( $post->post_date ) ) ); $data['post_title'] = 'g-'.date_i18n( 'Y-m-j-G-i-s', strtotime( $post->post_date ) ); return $data; } else { return $data; } } ?>
The code works fine but I have two questions now:
1. Update Post also changes title
Every time I update my post it overwrites the title with a new timestamp title. How can I stop it from overwriting and “only apply this if the post title has ‘no title'”. Otherwise it keeps changing the post title every time I update my post?2. Hierarchical Structure granpdarent > parent > child
I want to use a hierarchical post structure:
‘g-2014-01-01-10-00(grandparent)’ > ‘p-2014-01-01-10-10(parent)’ > ‘c2014-01-01-10-20(child)’.I would like the child of a post to get the parents title as a prefix to its title name:
Result Titles:
Grandparent Title:
‘g-2014-01-01-10-00’
Parent Title:
‘g-2014-01-01-10-00-p-2014-01-01-10-10’
Child Title:
‘g-2014-01-01-10-00-p-2014-01-01-10-10-c-2014-01-01-10-20’.I hope this all makes sense. Thank you for your help ??
- The topic ‘Auto Generate Title for Custom Post / inherit parent page title’ is closed to new replies.