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());

JoshStauffer.com runs on the Genesis Framework

Genesis Framework

Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!

Take advantage of the 6 default layout options, comprehensive SEO settings, rock-solid security, flexible theme options, cool custom widgets, custom design hooks, and a huge selection of child themes ("skins") that make your site look the way you want it to. With automatic theme updates and world-class support included, Genesis is the smart choice for your WordPress website or blog.

Become a StudioPress affiliate

Comments

  1. Dave says:

    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. Ray says:

    Absolutely brilliant. I searched for this for half an hour before I read your post and it works perfectly. Thank you!

  3. Trevor says:

    Great approach! Super simple. I like it.

  4. mark says:

    Thank you Josh, works great!

  5. AJ Clarke says:

    Well this is definately a nice and easy way to do it. Thank you very much.

  6. Joseph says:

    Nice approach. Very clever. Thanks for posting it!

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

  8. Erin says:

    Slick solution. Thanks a ton!

  9. Brian says:

    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.

  10. lazysand says:

    It’s so cool! :D

  11. Hotelove says:

    Very nice, I was searching how to get slug for parent page :) Thanks

  12. Michelle Rupisan says:

    You saved me Josh :) thanks very brilliant

  13. justin says:

    Thank you so much! I have been looking for this for over a day. Thank you!

  14. Techjacker says:

    very nice!

    thanks for posting this

  15. Juan Ramos says:

    Yup this is definitely the best way to do this, thank you sir

  16. Jonathan Gallivan says:

    Simple and clean … thank you!

  17. Design1 says:

    Thanks for that! Really simple.
    Thanks a lot

  18. od3n says:

    I use this within the loop.

    <?php $post->post_name; ?>

  19. Steve says:

    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];

    • Steve says:

      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];

    • emil says:

      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.

  20. Steve says:

    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.

  21. Hey, it is great, does basename() function do heavy work like string operations? I mean, less code but better performance?

  22. JW says:

    This is EXACTLY what I was looking for. Thank you so much for posting this!!

  23. Matt says:

    Worked like a treat, thanks!

  24. dlare says:

    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

  25. Kevin Leary says:

    Great approach Josh!

    It’s nice to see some of the built-in PHP functionality being used as it should!

  26. Steffen says:

    Josh, thanks a lot for this, it saved me hours of my time to figure this out. Much appreciated!

  27. Hilmon says:

    Wonderful tip!!! …. Its people like you that make WP such a joy to work with :-)

    Many Thanks!

    :-p

  28. Steffi says:

    Very useful. Thanks!

  29. Takezo316 says:

    Genious!.. simply brilliant. Thxs a lot

  30. Chrome Orange says:

    PERFECT!

  31. Andrew says:

    You are a savior. This was driving me nuts, yours works perfect.

  32. Ben says:

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

  33. Ali says:

    Thank you very much. My problem solved.

  34. Paul says:

    Mine too …. Wonderfully simple!! Thanks!

  35. Chad says:

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

  36. Rutwick says:

    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

  37. Phil Lowe says:

    You are a hero. Thanks so much!

Speak Your Mind

*