As per other answers slug is stored in post_name property. While it could be accessed directly I prefer the (underused) get_post_field() function for access post properties which have no proper API for them.
It requires post provided explicitly and doesn’t default to the current one.
If you want to get slug of the post outside the loop then use:
$post_id = 20; //specify post id here
$post = get_post($post_id);
$slug = $post->post_name;
If you want to get slug of the post from the loop then use:
Inside of your loop you can do:
Another option is getting the slug by post ID:
Here is more info about
get_post_field
https://codex.wordpress.org/Function_Reference/get_post_fieldOutside the loop:
As per other answers slug is stored in
post_name
property. While it could be accessed directly I prefer the (underused)get_post_field()
function for access post properties which have no proper API for them.It requires post provided explicitly and doesn’t default to the current one.
If you want to get slug of the post outside the loop then use:
If you want to get slug of the post from the loop then use: