How to use custom fields in post title?

What you’re looking for are the filters that let you modify the page title. There is a new way of doing this since WordPress 4.4, so I’m going to focus on that. If you happen to be using a version prior to 4.4 and you can’t update, the alternative is the wp_title filter (not to be confused with the wp_title() function), which I can elaborate on if you’d like.

So, the filter you are after (for WP 4.4+) is called document_title_parts.

To use this, you want to open your theme’s functions.php file and place in the following:

add_filter("document_title_parts", "wpse_224340_document_title");

function wpse_224340_document_title($title){
    global $post; // make sure the post object is available to us
    if(is_singular()){ // check we're on a single post
        $release_author = get_post_meta(get_the_ID(), "release_author", true);
        if($release_author != ""){ $title["title"].= " (" . $release_author. ")"; }
    }
    return $title;
}

You’ll also need to remove that <title></title> tag that you found in your header.php, and add the following code to your functions.php as well:

add_filter("after_setup_theme", function(){ add_theme_support("title-tag"); });

This is telling WordPress that it’s safe to add its own <title> tag, which you’re then modifying with the document_title_parts filter above.

EDIT: As this is a fairly new change to WordPress, if you’re interested in the background behind it or you want to know why it’s best not to use wp_title anymore, you can see this blog post.

Leave a Comment