I find many plugins for displaying a list of related posts after a post.

However they are all based on taxonomies or text content.

YARPP – Yet Another Related Posts Plugin is an example of such plugins.

Is there any way to display "related" posts based on the links already present in the article?

For example: an article has link1 and link2 in its main content. Is it possible to display the link1 and link2 again at the bottom of the post (as if they were "related posts", with the article title and featured image)?

  • are you looking for a plugin or are you comfortable writing your own code?
    – Ben
    Aug 4 at 15:21
  • It's the same. A plugin would be convenient if it exists, otherwise custom code.
    – collimarco
    Aug 4 at 15:23
  • I do not have a plugin, but just write some JQuery to get the post ID, get the post content from the WP API, search the content for links and populate a div below the post that is defined as blank on the single template or in the post, and populate the div with the links found by the JQuery.
    – Ben
    Aug 4 at 15:33

The way I would approach this would be to create a template part called related-posts.php or something of the sort. In it, I would go through every link in the post's content and check to see if that url corresponds to another post (using the url_to_postid() function). I would then save all the non-zero id's to an array and finally, I would run a secondary wp loop to display those posts the way you want to (ie. thumbnail + title). Something like this:

(This code was partially modified from this answer)

$cont = get_the_content();
$related_post_ids = array();

//Only proceed if there is content
if ( !empty( $cont ) ) {
    // Get page content
    $html = new DomDocument;
    libxml_use_internal_errors(true); //See https://stackoverflow.com/questions/9149180/domdocumentloadhtml-error
    $html->loadHTML( '<?xml encoding="UTF-8">' . $cont );
    $html->preserveWhiteSpace = false;

    // Loop through all content links
    foreach( $html->getElementsByTagName( 'a' ) as $link ) {

        // Get link href
        $link_url = $link->getAttribute( 'href' );

        //If link href is not empty
        if( !empty( $link_url ) ){
            //Try to find the post that is being linked
            $linked_postid = url_to_postid( $link_url );

            //If we find the post, add it to the related_post_ids array
            if($linked_postid != 0){
                array_push($related_post_ids, $linked_postid);

//If we found other posts linked in the content
if( count($related_post_ids) > 0 ){
    //Run a secondary loop to display the related posts
    $q_args = array( 
        'post_type' => 'any',
        'post_status' => 'publish'
        'posts_per_page' => 3 //Limit how many you show
        'post__in' => $related_post_ids, //Get only the posts we found
        'orderby' => 'post__in' //Order them in the same order as the links appear in the post's content

    $relposts_query = new WP_Query( $q_args ); 
    if ( $relposts_query->have_posts() ) : 
        // Start the Loop 
        while ( $relposts_query->have_posts() ) : $relposts_query->the_post(); 
            //Add your markup here, you may use the_post_thumbnail( ) for the featured image and the_title( ) for the article title
     * You may want to write some backup code here, what to display if the post in question
     * did not have any links to other posts.

I have not tested this code so it may have some errors I did not anticipate. However, it should be enough to guide you in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.