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. Absolutely brilliant. I searched for this for half an hour before I read your post and it works perfectly. Thank you!

  3. 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

  4. 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.

  5. Very nice, I was searching how to get slug for parent page ๐Ÿ™‚ Thanks

  6. 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.

  7. 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.

  8. 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

  9. Wonderful tip!!! …. Its people like you that make WP such a joy to work with ๐Ÿ™‚

    Many Thanks!

    :-p

  10. 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. ๐Ÿ™‚

  11. 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 );

  12. 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

  13. 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

Comments are closed.