The get_page_by_title() function has been deprecated in WordPress 6.2 – how to fix your code / the best alternative

WordPress 6.2 deprecated the get_page_by_title() function in favor of using WP_Query. WP_Query, unlike the deprecated function, can only be called after plugins and pluggable functions have been loaded, either on the plugins loaded action or later.

WP_Query is a bit more complicated than get_page_by_title(), but it offers greater flexibility. We will demonstrate how to use WP_Query and how to convert any get_page_by_title() functions in your existing codebase.

Replacing get_page_by_title() with WP_Query

With the old get_page_by_title() function, we’d have to use the following code to get the “Sample Page” content:

$page = get_page_by_title( 'Sample Page' );
$page_content = $page->post_content;
echo $page_content;

We need to provide a little more information to the WP_Query function. The advantage of this is that it is far more flexible and is not limited to pages. Here’s an example that will produce the same result as above:

$query = new WP_Query(
        'post_type'              => 'page',
        'title'                  => 'Sample Page',
        'posts_per_page'         => 1,
        'no_found_rows'          => true,
        'ignore_sticky_posts'    => true,
        'update_post_term_cache' => false,
        'update_post_meta_cache' => false,
if ( ! empty( $query->post ) ) {
    $fetched_page = $query->post;
    echo $fetched_page->post_content;
} else {
    $page_got_by_title = null;

Some of the parameters passed to the function in the preceding example are optional, but recommended for improved performance. The code performs the following functions:

  1. We’re instantiating WP_Query, selecting pages with a title of ‘Sample Page’. We’re limiting the results to one and ignoring sticky posts. no_rows_found, update_post_term_cache and update_post_meta_cache are setup here for better performance.
  2. The query is stored in the $query variable.
  3. An if-else conditional checks if the query result ($query->post) is not empty:
    • If not empty, the $fetched_page variable is set to $query->post, which contains the queried page. We then echo this.
    • If empty, the $fetched_page variable is set to null, indicating that no page with the specified title was found.

Here’s some (but not all) parameters that you can pass to WP_Query:

  • ‘post_type’: The type of post to query; in this case, ‘page’.
  • ‘title’: The title of the page we are searching for – in the example above ‘Sample Page’.
  • ‘post_status’: The status of the post; ‘all’ means it will search for pages with any status.
  • ‘posts_per_page’: The number of results to return; set to 1 since we are looking for a single page.
  • ‘no_found_rows’: If true, it skips the count of total rows found; set to true to improve performance.
  • ‘ignore_sticky_posts’: If true, it ignores sticky posts; set to true if you’re searching for a specific page.
  • ‘update_post_term_cache’: If false, it skips the update of term cache; set to false to improve performance.
  • ‘update_post_meta_cache’: If false, it skips the update of post metadata cache; set to false to improve performance.
  • ‘orderby’: The columns to sort by; in this case, ‘post_date’ and ‘ID’. This is unnecessary if only selecting one post.
  • ‘order’: The order of sorting, either ascending (ASC) or descending (DESC); This is unnecessary if only selecting one post.

Getting data other than the content

You don’t just have to fetch the content using WP_Query, you get a whole query object with a bunch more data that you can use. In the above example you could also use:

$fetched_page->post_author; // gets the author ID
$fetched_page->post_date; // gets the post date
$fetched_page->menu_order; // gets the menu order, if set

Wrapping it up

Finally, while the deprecation of the get_page_by_title() function in WordPress 6.2 may appear to be a barrier at first, the switch to utilising WP Query has substantial advantages. While WP_Query appears to be more complicated, its flexibility and interoperability with plugins and pluggable functions make it a more robust and powerful option for accessing the WordPress database.

We’ve shown you how to utilise WP_Query effectively and how to adapt existing get_page_by_title() methods in your codebase throughout this blog article. When you adjust your code to this change, you will most likely discover that the benefits of WP_Query, such as faster performance and more control over your queries, exceed the learning curve. By accepting this transition and harnessing the potential of WP_Query, you can continue to build efficient and dynamic WordPress websites that will last.