How to Find Internal Links to a Page or Post

Since I am working a lot with my website posts recently, from time to time I change the URLs of some specific articles or even set redirects to other articles.

When I do so, of course I use 301 redirects, but there still could be links from other posts and even comments to this particular post.

And in this tutorial I would like to show you how to easily find these kind of links in your website content and replace them.

At the same time we can rest a little bit from some complicated developer topics like Interactivity API and REST API which we took a look at in my previous blog posts.

Basically you can find internal links to an article in your WordPress admin if you go to Posts (if you want to find internal links from other posts) or Pages (internal links from pages) and so on. And then you need to type a slug of an article links to which you’re looking for in the search bar.

That’s it, it will show you the posts which have the links to this particular article:

Search for internal links to a post from WordPress admin.
Here we have found all the posts which contain a link to the “Hello world” post.

Of course the rest should be done manually, basically you will need to open every post and replace internal links in it. Slowly but surely.

If you need a more automatic solution, please continue to the next chapter.

Search and Replace in the Database

In this part you will need to either have an access to phpMyAdmin or an ability to install plugins on your website.

Let’s say that we only have the latter, in that case we need to go to “Plugins > Add New” and install a free “Database Management tool – Adminer” from there.

Then we open Adminer and go into “SQL command” section. Let’s run a SQL requests now, for example this one:

SELECT ID, post_title FROM wp_posts WHERE post_content LIKE '%http://test.rudrastyh.com/hello-world%'

Probably you already know how WordPress database works but just in case let me highlight a couple of moments for you:

  • In my SQL request I am asking the plugin to show me IDs and titles of the found posts,
  • FROM wp_posts means that we’re searching in the database table which contains your content. Please keep in mind that if you want to search internal links in comments, you need wp_comments database table. And also in your specific case the database prefix could be different, for example misha_posts or something.
  • The % symbol before and after the search string (URL) means that it could be inside the content.

And here we go:

Searching for internal links in the MySQL database
Technically we found two posts here which contain a URL of our target post – “Summer in Dubai” and some post without a title. A post duplicate which you can see here is just a revision.

As another example let’s try to find internal links to our target post in comments as well. In order to do so we can run the following SQL command.

SELECT comment_ID, comment_author FROM wp_comments WHERE comment_content LIKE '%http://test.rudrastyh.com/hello-world%'

And here we go:

Search for internal links to a post in comment content
So we found just one comment which contains a link to a target post.

SQL queries for search and replace

If you want not only to search for internal links in the post and comment content but also to replace them at the same time, below are the SQL queries for that.

For internal links in the post content:

UPDATE wp_posts SET post_content = REPLACE (post_content, 'URL1', 'URL2');

For internal links in comments:

UPDATE wp_comments SET comment_content = REPLACE (comment_content, 'URL1', 'URL2');

If you have internal links not only in post and comment content (maybe in custom fields), you can get more queries from this tool.

How do you think guys, would it be great to have a WordPress plugin which allows to do such a replacement right from the post edit pages?

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X