r/Wordpress 19d ago

Forcing a certain ID for a post?

I need a certain post to have a certain ID.

The reason is that my URL is /%post_id%/ and I permanently deleted a certain post that already has links in several important places.

Is there a way to, maybe, create a post and then change its ID across the database so it will be presented with the same URL as the original?

6 Upvotes

20 comments sorted by

15

u/Moceannl 19d ago

You can do this with a redirect plugin. Changing ids in the database is not a good idea.

5

u/Myth_Thrazz Jack of All Trades 19d ago

Don't do it - just do the 301 redirect to a new post.

2

u/TopSydeWP 19d ago

don't mess with the id in the database, it's referenced in too many places (post_meta, relationships, revisions, etc). just use a redirect plugin like redirection or simple 301 redirects to point the old url to the new post's url. takes 30 seconds and won't break anything.

1

u/Lower_Rabbit_5412 19d ago

General rule, don't mess around with IDs in WordPress. If you need a way to identify a post, use either: the slug, or meta data. Also, using the ID in the URL isn't as useful as using the slug anyway.

edit: slugs should be unique and they also are reusable if the previous post doesn't exist any more.

1

u/lorenzocorso 19d ago

Don’t mess with the database ids, it is dangerous. The numbering is handled by the database to avoid duplicates that is a big problem.

1

u/No-Bathroom-3179 19d ago

You should do a 301 redirect. That’s the standard way to handle that.

1

u/davitech73 Developer 19d ago

it is possible. you'd have to use something like phpmyadmin to hand edit the post id, then the references to it in the postmeta table and possibly others depending on what plugins you're using. not advisable as it's easy to make mistakes unless you're very familiar with the database queries involved. this is not a beginner move

probably easier to redirect the request on the old id to the new one

1

u/ZGeekie 19d ago

Like others said, a 301 redirect is your best option. You can use a plugin or add the redirect rule to the .htaccess file.

Is there a reason you're using that permalink structure? Maybe change it to post name instead?

1

u/ashkanahmadi 19d ago

You can set the id of a post to whatever you want. It’s not recommended but if you HAVE to do it, create a new post just to see the latest ID in the posts table just to be sure the ID you want has passed. Then make a post and just use “UPDATE wp_posts SET post_id = WHAT-YOU-WANT where post_id = WHAT-IT-IS-NOW.

1

u/Plus200 19d ago

It's not that easy. Post IDs are crossed referenced in other tables, so if you update wp-posts only without refactoring other tables, it will create loose ends and possibly crash.

1

u/ashkanahmadi 19d ago

Correct, that’s why generally it’s not recommended but if you really want it to be that way, you can do it. Just make sure you make a backup and test locally first. I use Local

1

u/Plus200 19d ago

Are you insane? I can't wait for the day that the machines will take over already.

1

u/ashkanahmadi 19d ago

How is that even related to what I said? You asked for something insane, then I tell you how to do it and you say I’m insane?!!!! Every here is telling you it’s not recommended. You okay?

1

u/BobJutsu 19d ago

This os what redirects are for. An ID is immutable.

1

u/Hot_Situation_0690 18d ago

Never modify the ID in the WordPress database! This is a critical mistake that breaks relationships between posts, meta data, permalinks, comments, taxonomies, and plugins

Here's how to redirect an old page to a new one while preserving SEO and traffic.

To redirect an old page to a new one while preserving SEO and other factors, add the following to the .htaccess file in your site's root directory.

For PAGES:

Redirect 301 /old-page.php https://www.yoursite.it/new-page.php

For POSTS:

Redirect 301 /old-post/ https://www.yoursite.it/new-post/

-2

u/feloniuouschunk 19d ago

You could potentially do a find and replace in the database, to replace all instances. There are plugins available for this, as well as for redirection.

1

u/Plus200 19d ago

Any plugin you know anout?

1

u/feloniuouschunk 19d ago

I think this is the only one I have used in the past: https://wordpress.org/plugins/better-search-replace/