How to Get WordPress Page and Post Slug?

Because WordPress produces slugs for your pages and posts automatically, you should double-check that they are search engine friendly. You’ll need to grab the default slug to see how it looks and, if necessary, adjust it. We’ll show you how to get both the WordPress page and post slugs in this article.

How to Get WordPress Page Slug?

A WordPress page slug can be obtained in a variety of ways. The two ways we’re about to demonstrate don’t require a page ID. The procedures are as follows:

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

$post global;

$post->post_name = $page_slug;

After typing the lines above while running the application, you’ll get the page slug.

Method 2 – By using the get_post_field function

Please keep in mind that this strategy only works if you’re inside the loop. As long as you’re in the loop, you don’t need an ID. To acquire the WordPress page slug, use the code line below.

$get_post_field(post_name) $page_slug;

How to Get WordPress Post Slug

There are two other ways to get a WordPress post slug. No post ID is required for any of the methods. The two methods are as follows:

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

You can acquire the post slug using a method that is quite similar to the method we used to retrieve the page slug. We’ll also use the post_name property here, which requires first accessing the $post global variable. To get the post slug, please use the code lines below.

$post global;

$post->post_name = $post->post_slug;

When you compare the code lines for getting post slug to the code lines for getting page slug, you’ll notice that the only difference is that when you need to get post slug, $post_slug replaces $page_slug.

Method 2 – By using the get_post_field function

You should be within the loop before typing the code below if you don’t have the post ID. If you utilize this strategy, the post name is enough to help you acquire the post slog. You must enter the following code:

$get_post_field(post_name) $post_slug;

WordPress Get Page Slug by ID

If you wish to acquire a page slug outside the loop, you’ll have to utilize ID.

Here’s the code you’ll need to fetch the page slug by ID:

$get_post_field(post_name, $post_id) $slug;

Use the line below to get the page slug for a page with the ID of 3.

$get_post_field(post_name, 3) $slug;

WordPress Get Post Slug by ID

The post slug can be obtained using the code below.

$get_post_field(post_name, $post_id) $slug;

When you compare this line to the one for obtaining the page slug, you’ll notice that they’re identical. As long as you have the correct ID, the code line works for both page and post slugs. The post ID may always be found in the WordPress dashboard.

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

A good slug will boost your site’s search engine rankings. There are various characteristics that define an optimal slug. The following are the key characteristics of a good page or post slug:

It should be short

Short slugs are beneficial to both 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

At least one core term appears in each post. The main keyword must be included in the slug. When your slug contains important keywords, it will not only help your site rank higher in search results, but it will also provide humans a indication about what the URL offers before they click on it.

It should be descriptive

A descriptive slug will quickly summarize the content of the page or post. For example, you can use the title of a post or any sentence that summarizes it to describe it.

It should be permanent after publishing

You should not change the slug after a page or post has been published. Make sure you thoroughly review the slug before publishing it so you can catch anything that isn’t quite correct. If you change a slug after publishing a page or post, anyone who attempts to visit the page or post using the prior slug will be unable to do so.

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

Yes. While writing, you can change the slug of a page or a post. That is, in fact, the optimum time to change the slug. You can update the slug after the page or post you’re working on has a title. Check the space below the Title box to edit the slug while you’re still writing. The Permalink URL can be found below the box. Enter your preferred slug in the edit box beside the Permalink URL.

When choosing a slug for your page or post, make sure to consider all of the features of the ideal slug that we mentioned above.

Leave a Comment