How to Get the Page or Post Title in WordPress (PHP)

To get the current post or page title in WordPress, use get_the_title(). It returns the title as a string, takes an optional post ID or WP_Post object, and works for posts, pages and custom post types. Use the_title() to echo it instead.

// Return the current post's title
$title = get_the_title();

// Return the title of a specific post
$title = get_the_title( 42 );

// Echo it, wrapped in a heading
the_title( '<h1>', '</h1>' );

That covers most cases. The site name is get_bloginfo( 'name' ), an archive heading is get_the_archive_title(), and the browser tab is wp_get_document_title(). I ran every snippet below on WordPress 7.1.4 in WordPress Playground, and the outputs shown are what came back.


Which title function do you need?

Find the row that matches what you are after. Each one links to its section further down.

You wantUseReturns or echoesSection
Current post or page titleget_the_title()ReturnsCurrent title
Title of any post by IDget_the_title( $id )ReturnsBy ID
Print the title in a templatethe_title( '<h1>', '</h1>' )EchoesComparison
Title inside an attributethe_title_attribute()Echoes (or returns)Attributes
Raw title, no filtersget_post_field( 'post_title', $id )ReturnsRaw title
Site nameget_bloginfo( 'name' ) / bloginfo( 'name' )Returns / echoesSite title
Category, tag, author or date archive headingget_the_archive_title()ReturnsArchives
Blog posts page titlesingle_post_title( '', false )ReturnsArchives
Browser tab / <title> tagwp_get_document_title()ReturnsDocument title
Find a post from its titlenew WP_Query( array( 'title' => '...' ) )Returns postsBy title
Title via the REST APItitle.rendered / title.rawJSON fieldREST API
Block theme templatePost Title block (core/post-title)RendersBlocks
WordPress title functions at a glance

What get_the_title() actually returns

Titles go through filters before you see them, and that changes the string more than most people expect. I created a post titled Andy's "Best" <em>Tips</em> -- more and read it back five ways:

CallOutput
get_post_field( 'post_title', $id )Andy's "Best" <em>Tips</em> -- more
get_the_title( $id )Andy&#8217;s &#8220;Best&#8221; <em>Tips</em> &#8212; more
the_title_attribute( array( 'echo' => false ) )Andy&#8217;s &#8220;Best&#8221; Tips &#8212; more
esc_html( get_the_title( $id ) )Andy&#8217;s ... &lt;em&gt;Tips&lt;/em&gt; ...
wp_get_document_title()Andy&#8217;s &#8220;Best&#8221; Tips &#8212; more &#8211; Site Name
Tested on WordPress 7.1.4

A few things fall out of that:

  • Straight quotes become curly-quote entities and a double hyphen becomes an em dash entity. That is wptexturize running on the the_title filter, alongside convert_chars, trim and capital_P_dangit. Comparing a filtered title against a plain string with === will fail for exactly this reason.
  • HTML in a title survives get_the_title(). If an editor can put tags in titles, escape when you print a variable yourself.
  • Password-protected posts come back as Protected: Secret Post and private posts as Private: Hidden Post. Core only adds those prefixes on the front end (the ! is_admin() check in get_the_title()), so you will not see them in wp-admin.
  • A post with an empty title, or an ID that does not exist, returns an empty string rather than false. the_title( '<h2>', '</h2>' ) prints nothing at all in that case, not an empty heading, because it bails when the title length is zero.

To drop the prefixes on the front end without touching anything else, filter the format string. Both filters receive the format first and the post second:

// Show protected and private titles without "Protected:" / "Private:"
add_filter( 'protected_title_format', function () {
    return '%s';
} );
add_filter( 'private_title_format', function () {
    return '%s';
} );

Get the current post or page title

Inside the WordPress loop, or in any template where the global $post is set, get_the_title() with no argument returns the current post’s title:

// Inside the loop
while ( have_posts() ) {
    the_post();
    echo '<h2>' . esc_html( get_the_title() ) . '</h2>';
}

It works the same in single.php, page.php and archive.php. Outside the loop on a single post or page, get_the_title( get_queried_object_id() ) is the safer call, because it reads the post WordPress is actually displaying instead of whatever $post was last set to by a secondary query.


Get a title by post ID

Pass an integer ID or a WP_Post object to get_the_title() to read any post’s title, current or not:

// Title of post ID 42
$title = get_the_title( 42 );

// Or pass a WP_Post object
$post_obj = get_post( 42 );
$title    = get_the_title( $post_obj );

This is the version to use in a widget, a shortcode callback, a custom WP_Query without the_post(), or anywhere the global $post is not reliable. If you need the ID first, get_the_ID() and the other post ID functions cover every way to find it. The same filters and Protected/Private prefixes from the table above apply here too.


the_title() vs get_the_title()

There are three common ways to get at a title, and they differ in whether they echo and whether filters run:

FunctionReturns or echoes?Filters applied?Typical use
get_the_title()Returns a stringYesStore, trim, concatenate or pass the title along
the_title()Echoes (returns when the third argument is false)YesPrint the title in a template, with optional $before and $after HTML
$post->post_titleProperty on the post objectNoRaw value, same as get_post_field( 'post_title' )
the_title(), get_the_title() and $post->post_title compared

Quick side-by-side:

// the_title() echoes, wrapped in HTML
the_title( '<h1 class="entry-title">', '</h1>' );

// ...or returns when $display is false
$heading = the_title( '<h1>', '</h1>', false );

// get_the_title() when you need to process it first
$short = wp_trim_words( get_the_title(), 5 );
echo esc_html( $short );

When in doubt, use get_the_title(). Reach for $post->post_title only when you specifically want to skip the the_title filter, and remember that plugins hooking that filter (translation plugins, for example) will then be skipped too.


Use a title inside an HTML attribute

When a title goes into an attribute like title="", alt="" or aria-label="", use the_title_attribute(). In the test above it stripped the <em> tags and returned only entity-encoded text, so a quote in the title cannot break your markup:

<img src="<?php echo esc_url( $image_url ); ?>"
     alt="<?php the_title_attribute(); ?>">

To return the value instead of echoing it, or to read a different post, pass an args array:

$safe_title = the_title_attribute( array(
    'echo' => false,
    'post' => 42,
) );

If you already have a title string, esc_attr( wp_strip_all_tags( $title ) ) gets you to the same place.


Get the WordPress site title

The site name is not a post title at all. It is the Site Title under Settings > General, and you read it with get_bloginfo() (returns) or bloginfo( 'name' ) (echoes):

// Site title
$site_title = get_bloginfo( 'name' );

// Tagline
$tagline = get_bloginfo( 'description' );

// Echo the site title directly
bloginfo( 'name' );

If you are after the site’s address rather than its name, use home_url() or site_url(); the difference between them is covered in how to get the current page URL in WordPress. On Multisite, get_bloginfo( 'name' ) returns the current site’s title, and get_site_option( 'site_name' ) returns the network’s name.


Get the archive page title

On a category, tag, author, date or custom taxonomy archive, get_the_title() returns the first post in the loop, not the archive. Use get_the_archive_title(). Here is what it returned in testing:

Archiveget_the_archive_title() output
Category “Tutorials”Category: <span>Tutorials</span>
Author “admin”Author: <span>admin</span>
Year 2026Year: <span>2026</span>
The name is wrapped in a span, the prefix is not

To remove the Category: / Tag: prefix, return false from the get_the_archive_title_prefix filter, added in WordPress 5.5. I tested this one: the category title came back as plain Tutorials, with no span either, since core only adds the span when there is a prefix:

// Drop "Category:", "Tag:", "Author:" and friends from archive titles
add_filter( 'get_the_archive_title_prefix', '__return_false' );

That beats the old trick of running a regex over the get_the_archive_title filter, which breaks on translated sites where the prefix is not English. If you only want the term or post type name, the dedicated functions skip the prefix entirely:

single_cat_title( '', false );        // category name
single_tag_title( '', false );        // tag name
single_term_title( '', false );       // any taxonomy term
post_type_archive_title( '', false ); // CPT archive

One page trips people up: the blog posts page set under Settings > Reading. It is a page, but WordPress runs the posts loop on it, so in testing get_the_title() inside the loop returned the newest post’s title. single_post_title() reads the queried object instead, which on that page is the page itself, and it returned Blog:

if ( is_home() && ! is_front_page() ) {
    $blog_title = single_post_title( '', false );
    // Same result: get_the_title( get_option( 'page_for_posts' ) )
}

Get a raw title without WordPress filters

get_the_title() applies protected_title_format, private_title_format and the the_title filter. For the value exactly as stored in the post_title column, use get_post_field():

$raw_title = get_post_field( 'post_title', 42 );

// Same value from a post object you already have
$raw_title = get_post( 42 )->post_title;

No Protected/Private prefix, no curly quotes, and no capital_P_dangit. It is the right choice for comparisons, exports, slugs and anything you send to an API. The same get_post_field() call reads other columns too, which is how getting the current page slug works. Escape the output yourself with esc_html() or esc_attr(), since nothing in this path does it for you.


Get the document title (what the browser tab shows)

The text in the browser tab and in search results is assembled from the post title, the site name, pagination and the tagline. Any theme that declares add_theme_support( 'title-tag' ) (every block theme does) prints it through wp_get_document_title(), and you can reshape it with filters:

// What WordPress prints inside <title> for this request
$document_title = wp_get_document_title();

// Change the separator (default '-')
add_filter( 'document_title_separator', function () {
    return '|';
} );

// Edit the parts before they are joined: 'title', 'page', 'tagline', 'site'
add_filter( 'document_title_parts', function ( $parts ) {
    $parts['site'] = 'SmartWP';
    return $parts;
} );

In testing, the default separator came out as &#8211; (an en dash), not a hyphen: core passes '-' to the filter, then texturizes the result. HTML tags in the post title were stripped from the document title. To replace the whole string, return a non-empty value from pre_get_document_title. If Yoast or Rank Math is active it already filters here, so change the plugin’s title settings rather than fighting it in code.

You may still see wp_title() in older themes. It exists in 7.1.4 and is not marked deprecated, but it predates title-tag support from WordPress 4.4, so new themes should not call it.


Find a post by its title

Sometimes you have the title and want the post. get_page_by_title() was deprecated in WordPress 6.2; calling it in 7.1.4 still works but fires a deprecation notice telling you to use WP_Query. The replacement is the title argument, added in 4.4:

$query = new WP_Query( array(
    'post_type'      => 'page',
    'title'          => 'About Us',
    'post_status'    => 'publish',
    'posts_per_page' => 1,
    'no_found_rows'  => true,
) );
$page = $query->have_posts() ? $query->posts[0] : null;

It compares against the raw post_title column with =, so spacing and punctuation must match, and a curly-quoted title from get_the_title() will not find anything. Case usually does not matter, because WordPress tables use a case-insensitive collation by default; my lowercase 'blog' found the page titled Blog. If you do this often, store the post ID instead. Titles change, IDs do not.


Get the title from the REST API

Every post in the WordPress REST API has a title object. Anyone can read title.rendered; title.raw only appears with ?context=edit for a user who can edit the post. From the same test post:

GET /wp-json/wp/v2/posts/42?context=edit

"title": {
    "raw": "Andy's \"Best\" <em>Tips</em> -- more",
    "rendered": "Andy&#8217;s &#8220;Best&#8221; <em>Tips</em> &#8212; more"
}

rendered is the output of the the_title filter, so it carries the same entities and HTML as get_the_title(). Decode it before you show it as plain text in JavaScript. One difference: the posts controller swaps the format filters out while it prepares the response, so a protected or private post’s rendered title came back without the Protected: or Private: prefix. Add _fields=id,title to keep the response small.


Block themes: the Post Title and Site Title blocks

In a block theme you rarely write any of this PHP. The Post Title block (core/post-title) renders the current post’s title from the postId block context, the Site Title block renders get_bloginfo( 'name' ), and the Query Title block covers archive headings. In template markup it looks like this:

<!-- wp:post-title {"level":1,"isLink":false} /-->
<!-- wp:site-title /-->

Rendered for the test post with "level":2 and "isLink":true, the block produced an h2 with the class wp-block-post-title and a link to the permalink, around the same texturized title get_the_title() returns. Its other attributes are levelOptions, rel, linkTarget and placeholder. It also kept the Protected: prefix on a password-protected post, so the format filters above apply to it as well.

Inside the block editor itself, JavaScript reads the title being edited with wp.data.select( 'core/editor' ).getEditedPostAttribute( 'title' ). The PHP functions still matter in plugins, shortcodes and custom blocks.


Frequently asked questions

How do I get the current page title in WordPress?

Use get_the_title() inside the loop or in a template where the global $post is set; it returns the title as a string. the_title() echoes it instead. Outside the loop, get_the_title( get_queried_object_id() ) reads the page WordPress is displaying.

How do I get a post title by ID in WordPress?

Pass the ID to get_the_title(): get_the_title( 42 ). It also accepts a WP_Post object and returns an empty string if the ID does not exist. For the unfiltered value use get_post_field( 'post_title', 42 ).

Why does my title start with Protected: or Private:?

get_the_title() adds those prefixes on the front end for password-protected and private posts. Return '%s' from the protected_title_format and private_title_format filters to remove them, or read the raw title with get_post_field().

Why does get_the_title() return HTML entities or curly quotes?

The the_title filter runs wptexturize, which turns straight quotes into curly-quote entities and -- into an em dash entity. Use get_post_field( 'post_title', $id ) when you need the title exactly as typed, for example to compare it with another string.

How do I get the site name in WordPress?

Use get_bloginfo( 'name' ) to return the Site Title from Settings > General, or bloginfo( 'name' ) to echo it. The tagline is get_bloginfo( 'description' ).

Why isn’t get_the_title() working on my archive or blog page?

On archives and the blog posts page, get_the_title() returns the first post in the loop. Use get_the_archive_title() for archive headings and single_post_title( '', false ) for the blog posts page.


Bottom line

Use get_the_title() for post and page titles, the_title_attribute() inside attributes, get_bloginfo( 'name' ) for the site name, get_the_archive_title() on archives and wp_get_document_title() for the browser tab. When the filtered output gets in your way, get_post_field( 'post_title', $id ) gives you the title exactly as it was saved.

Picture of Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 15 years. Through his years of experience has built 100s of sites and learned plenty of tricks along the way. Found this article helpful? Buy Me A Coffee

8 Responses

  1. Great it works, thank you very much.
    Small question, how can I dynamically retrieve the title ID if I decide to delete it and recreate another one from my plugin?

    Here is his code:
    add_action(‘init’, function () {
    register_post_type(‘titlefooter’, [
    ‘label’ => ‘Footer titles’,
    ‘public’ => true,
    ‘menu_position’ => 3,
    ‘menu_icon’ => ‘dashicons-ellipsis’,
    ‘supports’ => [‘title’],
    ‘show_in_rest’ => true,
    ‘has_archive’ => true,
    ]);

    });

      1. No, sorry, that doesn’t work…
        I will search again on the web hoping to find a solution for my Wordpress theme.
        If not, I’ll think of another way to do it.
        Thank you again for your help.

Leave a Reply

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

WordPress Tips Monthly
Get the latest from SmartWP to your inbox.