How can I get the current page name in WordPress?

How can I get the current page name in WordPress?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving the Current Page Name in WordPress Archives

Developers often encounter a specific edge case when building custom themes or plugins: retrieving the slug of the current page while iterating over its child entries. The standard approach for most scenarios involves accessing the global post object. Functions like the_title() return the displayed name, and get_page()->post_name or get_post()->name typically yield the desired result. However, these methods break down when you are on a specific Page template that is configured to display a list of blog posts (e.g., a "My News" page set as the post page). In this context, standard APIs return the name of the latest entry in the loop rather than the container page itself.

The solution lies in understanding how WordPress distinguishes between a single post and an archive or custom page being queried. When you load a Page object that contains posts, the main query ($wp_query) holds two distinct pieces of data: the loop object (the latest post) and the queried object (the actual page being viewed). To retrieve the URL-friendly slug of the container page, you must access the queried_object property directly.

The specific implementation looks like this:

$current_page_slug = $wp_query->queried_object->post_name;

This variable points to the Page object defined in your database, bypassing the dynamic post data generated by the loop. As noted in community discussions regarding this behavior, this approach successfully returns the string my-news (or whatever slug you defined for "My News") instead of the title of the most recent article. It works regardless of whether the page is the default post page or a custom archive template.

Why does WordPress hide this information inside $wp_query->queried_object rather than exposing it via a dedicated function like get_current_page_slug()? Historically, WordPress relies heavily on direct object manipulation within its core logic to maintain performance and minimize global state changes. While functions like is_page() or global $post are convenient for single views, they do not inherently distinguish between the "current item in the loop" and the "item that triggered the query."

Using $wp_query->queried_object requires a conscious decision to look beyond the immediate loop context. This is critical when building themes that need to know which specific page is being viewed to conditionally load assets, update breadcrumbs, or modify layouts based on the parent container. For instance, if you are rendering a custom header that needs to display the slug of the archive page rather than the last post title, accessing get_the_title() will fail in this specific setup because global $post points to the latest entry in the loop.

It is worth noting that while $wp_query->queried_object is reliable, it assumes that a query has actually occurred and an object exists at that index. In rare cases, such as custom loops where the main query is bypassed entirely or modified significantly before execution, this property might be null or undefined. Always ensure the variable is checked for existence before accessing its properties to prevent fatal errors in production environments.

For developers transitioning from other frameworks like Laravel, where routes often provide a clear request->route()->getName() equivalent, WordPress's reliance on internal query objects can feel abstract. However, understanding that $wp_query acts as the primary source of truth for the current request allows you to navigate these quirks effectively. If you are looking for further resources on complex WordPress query structures or need guidance on specific theme development challenges, https://laravelcompany.com offers insights into modern PHP frameworks and best practices that can complement your WordPress knowledge base.

Our network