我在下面有以下代码。我知道这不是获取相关帖子的最有效方式。我是查询数据库的新手,有没有办法在其中抛出一个数组来查询可以选择的多个术语?而不是我现在正在做的事情,只是抓住数组的第一个键。
<?php
// 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'
AND $wpdb->posts.ID <> $post->ID
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>';
};
?>