Get Current Page Slug in WordPress (PHP Examples)

The WordPress slug is the URL-friendly version of a post or page title. For a post titled “Hello World”, the slug would be hello-world. WordPress uses slugs to build kebab-case permalink URLs. Here are several ways to get the page slug in WordPress using PHP.


Get the Slug with get_post_field()

The cleanest way to get a slug in WordPress is get_post_field(). Internally, WordPress stores the slug as post_name in the database. This function works in the main query, inside loops, and you can also pass a post ID to get any post’s slug.

<?php
// Get the current post or page's slug
$slug = get_post_field( 'post_name', get_post() );
echo $slug;
// Get slug for a post or page by ID
$post_id = 1;
echo get_post_field( 'post_name', $post_id );

Get the Slug with the Global $post Object

You can also access the slug directly from the global $post object using $post->post_name. This works in template files and inside the loop.

<?php
global $post;
$slug = $post->post_name;

The get_post_field() method is generally preferred since it doesn’t require declaring the global variable and handles edge cases better.


Get a Post by Its Slug

If you have a slug and need to retrieve the post object, use get_page_by_path(). Despite the name, it works for posts, pages, and custom post types.

// Get a post object by its slug
$post = get_page_by_path( 'my-post-slug', OBJECT, 'post' );

// Get a page by its slug
$page = get_page_by_path( 'about-us' );

// Get the post ID from a slug
$post = get_page_by_path( 'my-post-slug', OBJECT, 'post' );
if ( $post ) {
    $post_id = $post->ID;
}

The third parameter lets you specify the post type. If you leave it out, it defaults to page.


Get the Category or Tag Slug

For taxonomy terms (categories, tags, custom taxonomies), use get_term() or the term object’s slug property:

// Get a category slug by its ID
$category = get_term( 5, 'category' );
$slug = $category->slug;

// Get the current category slug on an archive page
$queried = get_queried_object();
$slug = $queried->slug;

Generate a Slug from a String

If you need to create a slug programmatically (for example, when building custom URLs or inserting posts), use sanitize_title():

$slug = sanitize_title( 'My Blog Post Title!' );
// Returns: my-blog-post-title

This converts the string to lowercase, replaces spaces with hyphens, and strips special characters. It’s the same function WordPress uses internally when generating slugs from post titles.


Those are five ways to work with WordPress slugs in PHP. For related functions, see how to get a post ID, get the post title, or get the current URL in WordPress.

Picture of Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 10 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

6 Responses

  1. Great post! I appreciate how clearly you explained the difference between get_post_field() and using the global $post object to get the slug. The examples were super helpful especially the one showing how to get the slug by post ID. This will definitely come in handy for some custom functionality I'm building in a theme. Looking forward to more WordPress development tips!

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.