automatically add custom fields to post title

im going to add custom fields to post title ,

my post title is “Marroon Five” and my custom fields is “release-year” and with value “2017” .

so how to make the output : Marroon Five (2017)

this is what i found after doing some searching

add_action(‘save_post’, ‘update_term_title’);
function update_term_title($post_id)
{
if(defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE)
return;
if(!current_user_can(‘edit_post’, $post_id))
return;

//Replace ‘release-year’ with whatever your custom taxonomy slug is
$terms = wp_get_post_terms($post_id, ‘release-year’);

if(empty($terms))
return;

$title = false;
foreach($terms as $term)
{
if($term->parent)
{
$parent = get_term($term->parent, ‘muviyear’);
$title = $term->name.’ ‘.$parent->name;
break;
}
}
/*Default to first selected term name if no children were found*/
$title = $title ? $title : $terms[0]->name;

/*We must disable this hook and reenable from within
if we don’t want to get caught in a loop*/
remove_action(‘save_post’, ‘update_term_title’);
$update = array(
‘ID’=>$post_id,
‘post_name’=>sanitize_title_with_dashes($title),
‘post_title’=>$title
);
wp_update_post($update);
add_action(‘save_post’, ‘update_term_title’);
}

the only problem is this code replace all the post title with custom field value which is “2017”

hope someone can fix this is return output as i wanted to

thank you.

Read more here:: automatically add custom fields to post title

Leave a Reply

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