Aquí hay una solución que usa una variable estática para evitar un bucle infinito. Esto le permite llamar de forma segura a wp_update_post()
dentro de una función que está conectada a save_post
.
function km_set_title_on_save( $post_id ) {
// Set this variable to false initially.
static $updated = false;
// If title has already been set once, bail.
if ( $updated ) {
return;
}
// Since we're updating this post's title, set this
// variable to true to ensure it doesn't happen again.
$updated = true;
$date = get_post_meta( $post_id, 'rating_date', true );
$date_formatted = date( 'l, d.m.Y', strtotime( $date ) );
// Update the post's title.
wp_update_post( [
'ID' => $post_id,
'post_title' => 'TV ratings for ' . $date_formatted,
] );
}
add_action( 'save_post', 'km_set_title_on_save' );
Nota: para limitar esta funcionalidad a un determinado tipo de publicación, use save_post _ {$ post- > post_type }
gancho en lugar de save_post.