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) 
    if(!current_user_can('edit_post', $post_id))

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


    $title = false;
    foreach($terms as $term)
            $parent = get_term($term->parent, 'release-year');
            $title = $term->name.' '.$parent->name;
    /*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(
    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.

  • This should help: wordpress.stackexchange.com/a/198268/45202. The problem is caused by setting the ~$title~ to false instead of concatenating the term to the end of the existing title. Fetch the title, add to it and return it.
    – jdm2112
    Mar 7, 2018 at 16:16
  • sorry im not really understand , can you explain it a little bit sir
    – juicebyah
    Mar 7, 2018 at 17:03

1 Answer 1


You could write a function to hook into the display of the title, and then when the title is displayed, it will get the value of the custom field and append it to the title. That way you aren't editing/changing the title every time you save the post.

You could do something like:

function append_year( $title ) {
    global $post;
    $terms = wp_get_post_terms( $post->ID, 'release-year', array("fields" => "names") );

    if ( count( $terms ) ) {
        return $title . '(' . $terms[0] . ')';
    } else {
        return $title;

add_filter( 'the_title', array($this, 'append_title' ) );

wp_get_post_terms returns an array, and you can pass it an array of args to configure what you want see the wp_get_post_terms documentation.

The code above was adapted from this other SO post about appending title text

  • Fatal error: Uncaught Error: Using $this when not in object context got an error when put on functions.php mate
    – juicebyah
    Mar 8, 2018 at 16:24
  • Try it just as add_filter( 'the_title', 'append_title' ); Mar 9, 2018 at 21:56
  • sorry for the late reply , after i changed the code , no error on the output but the problem is still doesnt auto add spesific custom fields to post title , i think i have to do that manually
    – juicebyah
    Mar 15, 2018 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.