• Hi,

    I am using this snippet in my child theme functions.php to automatically generate a title (which I use as an identifier for an inventory) for my custom posts resulting in the prefix p’ and the ‘date’: eg Name: p-2014061184244 or Title: P-2014061184244.

    Problem is that every time I update my post it overwrites my previous post title. How can I stop it to overwrite if my title already has a value? eg if post already has title leave it – if post has no title generate title.

    <?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-custom-post-type') {
    
        $data['post_name'] = sanitize_title( 'p'.date_i18n( 'YmjGis', strtotime( $post->post_date ) ) );
    
        $data['post_title'] = 'P'.date_i18n( 'YmjGis', strtotime( $post->post_date ) );
    
        return $data;
        } else {
        return $data;
        }
        }
    
    ?>

    Thank you for your help ??

  • The topic ‘Auto Generate Title for Custom Post – Stop Overwright when updating post’ is closed to new replies.