Setting title using wp_title filter

Since wp_title() is usually called from the header.php file of your theme, then it runs on every page of your WordPress (frontend usually). So place the filter hook and function in your theme’s functions.php file, and just check if it’s a blog post before you change the title. Something like this:

add_filter('wp_title', 'filter_pagetitle');
function filter_pagetitle($title) {
    //check if its a blog post
    if (!is_single())
        return $title;

    //if you get here then its a blog post so change the title
    global $wp_query;
    if (isset($wp_query->post->post_title)){
        return $wp_query->post->post_title;
    }

    //if wordpress can't find the title return the default
    return $title;
}

Leave a Comment