Title replace using save_post is working but slug replace using publish_post not working (wp_update_post)

<?php
function set_title_on_save( $post_id ) {
$post_type = get_post_type($post_id);
if ( “event” != $post_type ) return;
static $updated = false;
if ( $updated ) {return;}
$updated = true;
$name = get_post_meta( $post_id, ‘event-name’, true );
$categories = get_the_terms( $post->ID, ‘venue’ );
if( ! empty( $categories ) )
{$venue = ‘ @ ‘ . esc_html( $categories[0]->name );}
wp_update_post( [
‘ID’ => $post_id,
‘post_title’ => $name . $venue,
] );
}
add_action( ‘save_post’, ‘set_title_on_save’ );

function set_name_on_save( $post_id ) {
$post_type = get_post_type($post_id);
if ( “event” != $post_type ) return;
static $updated = false;
if ( $updated ) {return;}
$updated = true;
$name = get_post_meta( $post_id, ‘event-name’, true );
$categories = get_the_terms( $post->ID, ‘venue’ );
if( ! empty( $categories ) )
{$venue = ‘-‘ . esc_html( $categories[0]->name );}
wp_update_post( [
‘ID’ => $post_id,
‘post_name’ => $name . $venue,
] );
}
add_action( ‘publish_post’, ‘set_name_on_save’ );

Read more here:: Title replace using save_post is working but slug replace using publish_post not working (wp_update_post)

Leave a Reply

Your email address will not be published. Required fields are marked *