97

I am trying to retrieve the slug of the current WordPress page outside the loop. The title of the page returns with wp_title (), but how can I get the slug?

<li>
  <a href="/slug-of-current-page/">
    <?php wp_title('', true); ?>
  </a>
</li>

11 Answers 11

150

Use the global variable $post:

<?php 
    global $post;
    $post_slug = $post->post_name;
?>
  • 3
    Thank you. Your solution works great. Just need to echo the slug: <?php global $post; $post_slug=$post->post_name; echo $post_slug; ?> – sarytash Feb 13 '12 at 12:13
  • 1
    Like sarytash said, you need to echo it. So, this'd be ideal: <?php global $post; echo $post->post_name; ?> – its_me Oct 11 '13 at 15:59
  • What about $WP_Post? – Peter Mortensen Apr 24 at 13:00
66

As per other answers, slug is stored in the post_name property. While it could be accessed directly, I prefer the (underused) get_post_field() function for accessing post properties which have no proper API for them.

It requires post provided explicitly and doesn't default to the current one, so in full for the current post it would be:

$slug = get_post_field( 'post_name', get_post() );
  • 12
    It is worth noting that if you are in the loop you can use get_post_field without second argument (docs) – jmarceli Jun 16 '16 at 6:42
25

EDIT 5 APRIL 2016

After digging for more reliability, I ended up doing this answer to the following post which leads to this edit: (Be sure to check it out)

The most reliable method till date I could come up with is the following:

// Get the queried object and sanitize it
$current_page = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() );
// Get the page slug
$slug = $current_page->post_name;

This way, you are 99.9999% sure that you get the correct data every time.

ORIGINAL ANSWER

Another safer alternative to this problem is using get_queried_object() which holds the current queried object to get the page slug which is held by the post_name property. This can be used anywhere in your template.

$post can be used, but it can be unreliable as any custom query or custom code can change the value of $post, so it should be avoided outside of the loop.

Using get_queried_object() to get the current page object is much more reliable and is less likely to be modified, unless you are using the evil query_posts which breaks the main query object, but then that is all up to you.

You can use the above as follow

if ( is_page() )
    $slug = get_queried_object()->post_name;
  • I must say that query_posts is not evil when you want to alter the main query, which however you usually don't and is often misused :) – jave.web Mar 3 '18 at 21:12
11

The simple way to get the slug is with:

<?php echo basename(get_permalink()); ?>
  • 2
    this depends on the permalink settings. If you use the "simple" setting, links will look like http://domain/?p=123, leaving you with ?p=123. – Mene Oct 14 '16 at 10:36
8

Given the code example, it looks like what you really need is a link. In that case, you can use get_permalink(), which can be used outside of the loop. That should do what you need more reliably than using the post slug.

  • 4
    This is the full URL though, not just the slug. – Fred Nov 21 '14 at 15:09
2

Might be an old question, but I created the functions get_the_slug() and the_slug() based on your answers.

if ( !function_exists("get_the_slug") ) {
    /**
    * Returns the page or post slug.
    *
    * @param int|WP_Post|null $id (Optional) Post ID or post object. Defaults to global $post.
    * @return string
    */
    function get_the_slug( $id = null ){
        $post = get_post($id);
        if( !empty($post) ) return $post->post_name;
        return ''; // No global $post var or matching ID available.
    }
    /**
    * Display the page or post slug
    *
    * Uses get_the_slug() and applies 'the_slug' filter.
    *
    * @param int|WP_Post|null $id (Optional) Post ID or post object. Defaults to global $post.
    */
    function the_slug( $id=null ){
        echo apply_filters( 'the_slug', get_the_slug($id) );
    }
}
0

Just further on @Matthew Boynes answer, if you're interested in getting the parent slug (if any) also then I've found this function useful:

function mytheme_get_slugs() {
    if ( $link = get_permalink() ) {
        $link = str_replace( home_url( '/' ), '', $link );
        if ( ( $len = strlen( $link ) ) > 0 && $link[$len - 1] == '/' ) {
            $link = substr( $link, 0, -1 );
        }
        return explode( '/', $link );
    }
    return false;
}

Eg to add the slug(s) to the body class:

function mytheme_body_class( $classes ) {
    if ( $slugs = mytheme_get_slugs() ) {
        $classes = array_merge( $classes, $slugs );
    }
    return $classes;
}
add_filter( 'body_class', 'mytheme_body_class' );
0

If you want a more under-the-hood answer, you can use the following SQL query to fetch all of the posts that are either posts, pages, or custom taxonomies at any time, even if no hooks have fired whatsoever as of yet.

Raw SQL:


SELECT `id`, `post_type` AS `type`, `post_author` AS `author`, `post_name` AS 
`slug`, `post_status` AS `status`
FROM wp_posts 
WHERE `post_type` NOT IN ('attachment', 'nav_menu_item', 'revision')
AND `post_status` NOT IN ('draft', 'trash')
ORDER BY `id`;

This works even on the very first line of your functions file, even prior to the mu_plugins_loaded or init hooks.

@note

This is assuming you have a standard database prefix wp_posts. If you need to account for variable prefixes, you can obtain the correct post table through PHP pretty easily by doing the following:

<?php
global $wpdb;
$table = $wpdb->posts;
$query = "SELECT `id`, `post_type` AS `type`, `post_author` AS `author`, `post_name` AS 
`slug`, `post_status` AS `status`
FROM " . $table . "
WHERE `post_type` NOT IN ('attachment', 'nav_menu_item', 'revision')
AND `post_status` NOT IN ('draft', 'trash')
ORDER BY `id`;"

Then run with either $wpdb, mysqli, or a PDO instance. Since there is no user input in this query, it is safe to run without a prepared statement as long as you do not inject any variables into it.

I would suggest storing this as a private static value of a class, so it can be accessed without having to fire the query again more than once per page for best performance, something like this:

class Post_Cache
{
    private static $post_cache;

    public function __construct()
    {
        //This way it skips the operation if it's already set.
        $this->initCache();
    }

    public function get($id, $type = null)
    {
        if ( !(is_int( $id ) && array_key_exists( $id, self::$post_cache ) ) )
            return false;
        }
        if ( !is_null( $type ) )
        {
            //returns the specific column value for the id
            return self::$post_cache[$id][$type];
        }
        //returns the whole row
        return self::$post_cache[$id];
    }

    private function initCache()
    {
        if ( is_null(self::$post_cache) )
        {

            $query = "...";
            $result = some_query_method($query); //Do your query logic here.
            self::$post_cache = $result;
        {
    }
}

Usage

$cache = new \Post_Cache();

//Get the page slug
$slug = $cache->get( get_the_ID(), 'slug');

if ($cache->get( get_the_ID() ))
{
    //post exists
} else {
    //nope, 404 'em
}
if ( $cache->get( get_the_ID(), 'status') === 'publish' )
{
    //it's public
} else {
    //either check current_user_can('whatever_permission') or just 404 it,
    //depending whether you want it visible to the current user or not.
}
if ( $cache->get( get_the_ID(), 'type') === 'post' )
{
    //It's a post
}
if ( $cache->get( get_the_ID(), 'type') === 'page' )
{
    //It's a page
}

You get the gist. If you need further details, you can fetch them as per normal with new \WP_Post( get_the_ID() );


This will let your check the posts at any time, even if the wordpress loop has not hit a point where it finds your request agreeable. This is a slightly more optimized version of the same query run by the Wordpress core itself. This one filters out all of the junk you would not want returned, and just gives you a nicely organized list with the relevant author id, post type, slug, and visibility. If you need further details, you can fetch them as per normal with new \WP_Post($id);, or use any of the other native Wordpress functions with any of the relevant table rows, even outside of the loop.

I use a similar setup in a couple of my own custom themes and plugins, and it works pretty great. It's also secure and doesn't leave internal data floating around in the global scope where it can be overridden like most stuff in Wordpress does.

0

This is the function to use when wanting to retrieve the slug outside of the loop.

get_post_field( 'post_name');

Answer found here: How to Retrieve the Slug of Current Page in WordPress?

0

I honestly don't understand why none of the answers simply do:

global $wp;
$current_slug = $wp->request;

// Given the URL of https://example.com/foo-bar
if ($current_slug === 'foo-bar') {
  // the condition will match.
}

This works for all posts, pages, custom routes.

-1

Dynamic Page calling in WordPress.

<?php
    get_template_part('foldername/'.basename(get_permalink()),'name');
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.