<?php
/*
Plugin Name: Intrasite Links
Plugin URI: http://ddhr.org/code/intrasite-links/
Description: Finds which posts link to the current post.
Author: David Hosier
Version: 0.7
Author URI: http://ddhr.org/
*/
function intrasite_links() {
$before = '<div class="comment post"><p><b>Linked:</b> ';
$after = '</p></div>';
$between = ', ';
global $wpdb, $post;
$thispostlink = str_replace(get_bloginfo('url'),"",get_permalink());
$results_posts = $wpdb->get_results("
SELECT ID, post_title FROM $wpdb->posts
WHERE post_content LIKE '%$thispostlink%' AND ID != $post->ID AND post_status = 'publish'
GROUP BY ID
");
$results_comments = $wpdb->get_results("
SELECT comment_ID, comment_post_ID FROM $wpdb->comments
WHERE comment_content LIKE '%$thispostlink%' AND comment_type != 'pingback'
GROUP BY comment_ID
");
if ($results_posts || $results_comments) {
echo $before;
if($results_posts) {
foreach ($results_posts as $result) {
echo '<a href="'.get_permalink($result->ID).'">'.$result->post_title.'</a>';
if ($result != end($results_posts)) echo $between;
}
}
if ($results_posts && $results_comments) echo $between;
if ($results_comments) {
foreach ($results_comments as $result) {
$the_post_title = $wpdb->get_row("SELECT post_title FROM $wpdb->posts WHERE ID = $result->comment_post_ID");
echo '<a href="'.get_permalink($result->comment_post_ID).'#comment-'.$result->comment_ID.'">'.$the_post_title->post_title.' (comment)</a>';
if ($result != end($results_comments)) echo $between;
}
}
echo $after;
}
}
?>