'
',
'after_widget' => '',
'before_title' => '',
));
}
function wp_get_related_posts() {
global $wpdb, $post,$table_prefix;
$now = current_time('mysql', 1);
$tags = wp_get_post_tags($post->ID);
//print_r($tags);
$taglist = "'" . $tags[0]->term_id. "'";
$tagcount = count($tags);
if ($tagcount > 1) {
for ($i = 1; $i <= $tagcount; $i++) {
$taglist = $taglist . ", '" . $tags[$i]->term_id . "'";
}
}
$q = "SELECT p.ID, p.post_title, p.post_date, p.comment_count, count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='post_tag' AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id = p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < '$now' GROUP BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC $limitclause;";
//echo $q;
$related_posts = $wpdb->get_results($q);
$output = "";
if (!$related_posts) {
$related_posts = wp_get_most_commented_posts("LIMIT 10");
foreach ($related_posts as $related_post ) {
$output .= '';
$output .= ''.wptexturize($related_post->post_title).'';
$output .= '';
}
$output = '';
$output = "". $output;
return $output;
}
foreach ($related_posts as $related_post ) {
$output .= '';
$output .= ''.wptexturize($related_post->post_title).'';
$output .= '';
}
$output = '';
$output = "Related Posts
". $output;
return $output;
}
function wp_related_posts() {
$output = wp_get_related_posts() ;
echo $output;
}
function wp_get_most_commented_posts($limitclause="") {
global $wpdb;
$q = "SELECT ID, post_title, post_date, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish' GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC $limitclause";
return $wpdb->get_results($q);
}
Most Commented