I am currently using a custom query to grab related posts. However I was curious if there is a way to not grab the current post I am currently on. My only thought is to use the ID of the post and exclude it somehow. Is this possible?
Here is my current code pulling related posts:
// Get The Related Term
$terms = array();
foreach(wp_get_object_terms($post->ID, 'series') as $term){
$terms[] = $term->slug;
};
// Grab The First Term From The Array
$related_term = array_shift(array_values($terms));
// Query The Related Posts
$related_posts = $wpdb->get_results(
"
SELECT *
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = 'sermon'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'series'
AND $wpdb->terms.slug = '$related_term'
ORDER BY $wpdb->posts.post_date DESC
"
);
foreach ($related_posts as $related) {
echo '<li><a href="'.get_permalink($related->ID).'">'.get_the_title($related->ID).'</a></li>';
};
You can use the not equal clause <> in the where query. Something like
AND $wpdb->posts.ID <> $post->ID