I have the following function in functions.php in a child theme:
<?php
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 1 ); // Grabs the inserted post data so you can modify it.
function modify_post_title( $data ){
if($data['post_type'] == 'post' || $data['post_type'] == 'remark' || $data['post_type'] == 'share' && !get_page_by_title($data['post_title'],OBJECT, 'post')){
$id = get_the_ID();
$new_title = get_the_time( 'Y-m(M)-d(D)—HiT', $id );
$new_slug = get_the_time( 'Y-m-d-HiT', $id );
$data['post_title'] = $new_title;
$data['post_name'] = strtolower($new_slug); // Updates the post slug to the new title.
}
return $data;
}
What this should do is grab the (publish) date and time and set that to the title and slug for the listed post types. This worked precisely as expected until recently. For some reason, this function no longer works in WordPress 6.x (as of writing this, WordPress 6.0.3).
Nothing relevant seems to appear in any logs. What change or changes caused this to break? and what needs to be updated to get it working again?
get_the_ID
in this context isn't correct, it's for use inside query loops. If the ID isn't stored inside$data
, increase your filter accepted args to two and check the$postarr
(second incoming array of data) for the appropriate ID.