0

我目前正在使用自定义查询来获取相关帖子。但是,我很好奇是否有办法不抓住我目前正在发布的当前帖子。我唯一的想法是使用帖子的 ID 并以某种方式排除它。这可能吗?

这是我当前提取相关帖子的代码:

// 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>';
};
4

1 回答 1

2

您可以在 where 查询中使用不等于子句 <>。就像是

AND $wpdb->posts.ID <> $post->ID
于 2013-01-29T21:10:49.067 回答