You can achieve your desired result with the help of given code which needs to be added to functions.php file
of your theme.
The given code will automatically convert plain text titles in your post content into links pointing to the corresponding posts.
<?php
function auto_link_post_titles( $content ) {
// This is to get all published posts from all post types.
$args = array(
'post_type' => 'any', // this is to get posts from all post types.
'post_status' => 'publish',
'posts_per_page' => -1, // this is to retrieve all posts
);
$posts = get_posts( $args );
// Here are looping through each post and replace the title in the content with a link.
foreach ( $posts as $post ) {
$post_title = esc_html( $post->post_title ); // Here we are getting the post title.
$post_link = esc_url( get_permalink( $post->ID ) ); // Here we are getting the post link.
// Create a link element
$link = '<a href="' . $post_link . '">' . $post_title . '</a>';
// Here we are replacing the plain text title with the link.
$content = str_replace( $post_title, $link, $content );
}
return $content;
}
// We have apply the function to the content of posts.
add_filter( 'the_content', 'auto_link_post_titles' );