Get Post Slug in WordPress

I needed to get just the post slug for a project I have been working on. I found several sites that had similar ways of getting the post slug but I came up with an approach that utilizes WordPress’ get_permalink() and PHP’s basename().

The common approach:


$post_obj = $wp_query->get_queried_object();
$post_ID = $post_obj->ID;
$post_title = $post_obj->post_title;
$post_slug = $post_obj->post_name;

My approach:


$slug = basename( get_permalink() );

52 thoughts on “Get Post Slug in WordPress”

  1. This so very nearly fixed my problem, but I cannot get it to work properly outside of the loop. Is there a way of getting the actual url of the page and stripping it with just php?

  2. Thanks for this code, very handy. Just noticed though that the second method doesn’t return the slug if the page / post is the nominated home page, it just returns the root folder. Hope this helps, Charlotte

  3. Just what I was looking for.
    I searched long and hard for slug, when it’s post_name and permalink.
    How about the temporary permalink when it’s still a draft?
    Permalink returns the now permalink version.

  4. Am I missing something here? If you are within the loop, isn’t the easiest thing to use @od3n’s suggestion above: $post->post_name

    If you are using get_permalink(), don’t you have to be in the loop? If you are, then I would think that @od3n’s suggestion is the fastest and most simple.

    Here’s another suggestion that I believe should work most of the time with the bonus that you can get the parent slug too:

    $path_arr = explode(‘/’,$_SERVER[‘REQUEST_URI’]);
    $page_slug = $path_arr[3];
    $parent_page_slug = $path_arr[2];

    1. This is an update of my previous comment. If your WordPress install is not in the root of the server, you’ll want to use this instead:

      $section_path_arr = explode(‘/’,str_replace(get_bloginfo(‘wpurl’),”,$_SERVER[‘REQUEST_URI’]));
      $page_slug = $path_arr[2];
      $parent_page_slug = $path_arr[1];

    2. Well that does work, but if you have any other permalink settings than the default the order in the array will be different. So I would be careful writing something like that in any other case than on your own site, that way you will have full control over this all the time.

      Also – thanks for a neat tip, worked very well outside the loop as well for me.

  5. I should add that if you are in the loop, you should also be able to use this code to get the parent:

    $parent_page = get_post($post->post_parent);
    $parent_page_slug = $parent_page->post_name;

    Keep in mind that using this method though as opposed to the one I showed previously will probably run a little slower because you have to query the database. Database queries in PHP apps, are almost always slower than manipulating arrays and such. Not a big deal for a low trafficked site though.

  6. very nice indeed! one question though… any idea to do this backward? i.e. to get an id of a slug? i was hoping to use it in “exclude=” since it only accepts id. thanks

  7. Came across this page during a brain fart, only to find a better way to do it. Thanks man.

    FYI, I was wowed by the animated header, nice work. πŸ™‚

  8. Very handy. I just made use of this as a way to query a custom taxonomy. Thanks Josh.


    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $slug = basename(get_permalink());
    $args = array(
    'post_type'=> 'project',
    'work_type' => $slug,
    'posts_per_page' => 12,
    'order' => 'ASC',
    'paged' => $paged );
    query_posts( $args );

  9. Thanks Josh! This is a neat solution! Using ‘get_post’ returns the entire post object, which sounds like an overkill just to get the slug. They should add a function to get the post slug from the id !

    – Rutwick

  10. This doesn’t work with the default parameterized link structure (?page_id=21 .. etc)

    I came across this:
    sanitize_title(get_the_title());

    which works just as well

Leave a Reply

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