How to Get WordPress Page and Post Slug?

How to Get WordPress Page and Post Slug?
Share:

Since WordPress automatically generates slugs for your pages and posts, you might want to verify whether the slugs are friendly for search engines. You’ll have to get the default slug in order to see what it looks like or modify it if necessary. In this article, we’ll show you how to get both WordPress page slug and post slog.

How to Get WordPress Page Slug?

You can get a WordPress page slug using different methods. The two methods that we are about to show you don’t require a page ID. Here are the methods:

Method 1 – By accessing the post_name property within the $post global variable 

You can get the slug of any page through the post_name property. Fortunately, the post_name property isn’t difficult to access. The post_name is always saved within the $post global variable, so we must access the $post global variable before we can see the post_name property. Kindly use the code lines below to access the $post global variable and post_name property.

global $post ;

$page_slug = $post->post_name ;

You’ll get the page slug after typing the lines above while you are executing the application.

Method 2 – By using the get_post_field function

Kindly note that this method only works when you are inside the loop. You do not require an ID as long as you are in the loop. Use the code line below to get the WordPress page slug.

$page_slug = get_post_field( ‘post_name’ ) ;

How to Get WordPress Post Slug

You can get a WordPress post slug using two methods also. None of the methods requires post ID. Here are the two methods:

Method 1 – By accessing the post_name property within the $post global variable

You can get the post slug using an approach that’s very similar to the one we used while getting the page slug. We will make use of the post_name property here also, which means we need to access the $post global variable first. Kindly use the code lines below to get the post slug.

    global $post ;

    $post_slug = $post->post_name ;

If you compare the code lines for getting post slug to the one for getting page slug, you’ll realize that the only difference is that “$post_slug” replaces “$page_slug” when you need to get post slug.

Method 2 – By using the get_post_field function

If you don’t have the post ID, you should be inside the loop before you type the code below. The post name is enough to help you get the post slog if you use this method. Here is the code you need to enter:

$post_slug = get_post_field( ‘post_name’ ) ;

WordPress Get Page Slug by ID

You’ll have to use ID whenever you want to get a page slug outside the loop.

Here is the line you need in order to get page slug by ID:

$slug = get_post_field( ‘post_name’ , $post_id ) ;

So, if you want to get the page slug for a page with the ID of 3, you should use the line below.

$slug = get_post_field( ‘post_name’ , 3 ) ;

WordPress Get Post Slug by ID

You can get the post slug using the line below.

$slug = get_post_field( ‘post_name’ , $post_id ) ;

If you compare this line to the one for getting page slug, you’ll realize that they are exactly the same. The code line works for both page slug and post slug as long as you have the right ID. You can always check the post ID in the WordPress dashboard.

What Should an Ideal Page Slug or Post Slug Look like?

An ideal slug will help your site to rank higher on search engines. There are several things that make up an ideal slug. Here are the major features of an ideal page slug or post slug:

It should be short

Short slugs are beneficial to both humans and search engines. Short slugs make the entire URL short and are therefore easier to understand. Also, they display excellently on search engine results even on devices with small screen sizes.

It should contain the main keywords

Every post has at least one main keyword. An ideal slug must contain the main keyword. When slug contains main keywords, it will not only help your site to rank high in search results, but it will also hint humans on what the URL has to offer even before they click on it.

It should be descriptive

A descriptive slug will summarize what the page or post is all about at a glance. For instance, you can describe a post using its title or any sentence that summarizes the post.

It should be permanent after publishing

Once you have published a page or post, you shouldn’t edit the slug anymore. Make sure you examine the slug very well before you publish it so that you’ll spot anything which isn’t right before publishing. If you change a slug after publishing a page or post, the page or post will no longer be accessible to anyone who attempts to check the page or post using the previous slug.

Can I Edit the Slug While Writing a Post or Page?

Yes. You can edit a page slug or post slug while you are still writing. In fact, that is the best time to edit the slug. Once the page or post you are working on has a title, you are free to edit the slug. To edit the slug while you are still writing, you should check the space below the “Title box”. You will see the Permalink URL below the box. Click the edit button beside the Permalink URL and enter your desired slug.

Make sure you consider all the features of the ideal slug that we mentioned above while you are choosing a slug to use for your page or post.

Share: