Wordpress中的相关帖子 - 不包括当前帖子

时间:2013-01-29 21:00:26

标签: wordpress wpdb related-content

我目前正在使用自定义查询来抓取相关帖子。但是我很好奇是否有办法不抓住我目前的帖子。我唯一的想法是使用帖子的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>';
};

1 个答案:

答案 0 :(得分:2)

您可以使用not equal子句&lt;&gt;在哪里查询。像

这样的东西
AND $wpdb->posts.ID <> $post->ID