function cattag_related_posts( $limit = 5, $display_posts = true, $display_pages = false, $before = '
  • ', $after = '
  • ', $notfound = false, $order = 'DATE_DESC', $exclude_ids = '' ) { global $wpdb, $post; // WordPress globals $wheres = array(); $wheres[] = "p.post_status = 'publish'"; $wheres[] = "p.ID <> $post->ID"; // ************************************************* // Prepare selection of posts and pages // ************************************************* if ( ($display_posts === true) AND ($display_pages === true) ) { // Display both posts and pages // $poststatus = "IN('publish', 'static')"; $wheres[] = "p.post_type in ('post', 'page')"; } elseif ( ($display_posts === true) AND ($display_pages === false) ) { // Display posts only // $poststatus = "= 'publish'"; $wheres[] = "p.post_type = 'post'"; } elseif ( ($display_posts === false) AND ($display_pages === true) ) { // Display pages only // $poststatus = "= 'static'"; $wheres[] = "p.post_type = 'page'"; } else { // Nothing can be displayed return $notfound; } // ************************************************* // Prepare exlusion of categories // ************************************************* $exclude_ids = preg_replace('/[^0-9,]/','',$exclude_ids); // remove everything except 0-9 and comma $exclude_ids_sql = ''; if ($exclude_ids != '') { // $exclude_ids_sql = 'AND post2cat.category_id NOT IN(' . $exclude_ids . ')'; } // ************************************************* // Put the category IDs into a comma-separated string // ************************************************* $catsList = ''; $count = 0; foreach((get_the_category()) as $loop_cat) { // Add category id to list if ($count == 0) { $catsList = $loop_cat->cat_ID; } else { $catsList = $catsList . ',' . $loop_cat->cat_ID; } $count++; } $wheres[] = "tr.term_taxonomy_id in ($catsList)"; // ************************************************* // Prepare order // ************************************************* switch (strtoupper($order)) { case 'RANDOM': $order_by = 'RAND()'; break; default: // 'DATE_DESC' $order_by = 'p.post_date DESC'; } // ************************************************* // SQL query. DISTINCT is here for getting a unique result without duplicates // ************************************************* // $sql = "SELECT DISTINCT posts.ID, posts.post_title // FROM $wpdb->posts posts, $wpdb->post2cat post2cat // WHERE posts.ID <> $post->ID // AND posts.post_status $poststatus // AND posts.ID = post2cat.post_id // AND post2cat.category_id IN($catsList) // $exclude_ids_sql // ORDER BY $order_by // LIMIT $limit"; $sql = "SELECT DISTINCT p.ID, p.post_title FROM $wpdb->posts p left join $wpdb->term_relationships tr on p.ID = tr.object_id where ". join(" and ", $wheres) . " order by $order_by LIMIT $limit"; $queryresult = $wpdb->get_results($sql); // ************************************************* // Return the related posts // ************************************************* $result = ''; if (is_array($queryresult) && count($queryresult) > 0) { foreach($queryresult as $tag_loop) { $result .= $before . '' . $tag_loop->post_title . '' . $after . "\n"; } $result = "\n" . '' . "\n" . $result; // Please do not remove this line. return $result; } else { return $notfound; } }