How to get rid of the “-2” suffix on a page

If you are reading this blog post, you probably came across this issue yourself. You try to set the slug of a page (or other post type), and WordPress is appending a “-2” suffix to the slug. You are searching for a page with this slug, but can’t find one. But where is the issue coming from?

The problem

Let’s say we have a website about Berlin, and we want to create a page about the “Brandenburger Tor” (Brandenburg Gate), we would give it that title, write some content and then publish the page. We would hope to get a URL like example.com/brandenburger-tor, but we get example.com/brandenburger-tor-2 instead. But why?

The cause: media library items

With WordPress 6.4, that was just released, attachment pages are gone. But they are not really. For those of you how don’t know them, attachment pages were frontend pages one could navigate to, and that would just show the title/slug and the attachment. In some themes, it would even show the user who uploaded the file and when. This is how it looks like in WordPress 6.3 with TwentyTwenty:

Frontend screenshot showing the title/slug "brandenburger-tor", the post meta information, the image in the content and the beginning of the comment area

As you can see here as well, those attachment pages have the ability to be commented. There might be some website that would use them, but for most, they were rather useless.

With WordPress 6.4, those pages have been “removed”. When you access the URL for them, they would just redirect to the attachment file.

But the pages are not really removed. They are just not accessible anymore. They still exist in the database and have a title/slug. Now if we upload an image with a name like brandenburger-tor.jpg to the media library, the slug brandenburger-tor will be used for the attachment page, even with WordPress 6.4 without those frontend pages.

The solution: changing the slug of the attachment page

Now, to “free” the slug for our page, we have to change the slug of the attachment page. So let’s see how we can do this.

Finding the page

First, we search for the attachment name (we can also search for the slug):

List of media items with the search term "brandenburger-tor" with one entry showing the image

Editing the slug for the attachment page

We then click on the media item. At the bottom right, we find some links for the media item. With the first one, we can open that attachment page (this redirects us to the media item with WordPress 6.4) and with the second one, we can “Edit more details”:

The "Attachment details" pop with the "Edit more details" link highlighted

This will open up the “Edit Media” view, where you can see the Permalink for the image:

The "Edit Media" view with the "Permlink" at the top

In older versions of WordPress, there was an “Edit” button next to it, but this button does not exist anymore. Instead, you have to use the “Slug” screen element (meta box), which you probably first have to “active”, using the “Screen Options”:

The "Screen Options" opened with the "Slug" checkbox highlighted and checked

Now you can scroll down the page to this new meta box and change the slug for the attachment page. You could simply attach a suffix like “-attachment-page” to it:

Meta box to change the "Slug" for the page and the "Update" button on top

After you have changed the slug, hit the “Update” button.

Change the slug of the page

Now you can finally navigate to the page and update the slug. WordPress should now no longer add the “-2” suffix to the permalink.

Bonus: using the WP-CLI

If you are using the WP-CLI, you can change the slug of the attachment page using the wp post update command.

First, you have to find the ID of the page. You can find this when you hover the “Edit more details” link, or you search for attachments using the WP-CLI as well:

$ wp post list --post_type=attachment
+----+-------------------+-------------------+---------------------+-------------+
| ID | post_title        | post_name         | post_date           | post_status |
+----+-------------------+-------------------+---------------------+-------------+
| 35 | brandenburger-tor | brandenburger-tor | 2023-11-19 16:09:23 | inherit     |
+----+-------------------+-------------------+---------------------+-------------+

Once you have the ID, updating the slug works like this:

$ wp post update 35 --post_name=brandenburger-tor-attachment-page
Success: Updated post 35.

Conclusion

Having a page with that “-2” suffix is really annoying. And when you name your images or other media files similar to page names and also upload them into the media library before creating the page, you might run into this issue.

My advice would be to either create the page first, before uploading the media files, or even better, use better and more descriptive (longer) media file names before adding them to the page or media library.

If you happen to have run into this issue, now you should know how to fix this manually.

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

Leave a Reply

Your email address will not be published. Required fields are marked *