WordPress,查询找不到相关帖子



我有三个帖子,两个具有标签 eGuide的帖子,其中一个具有标签 Article。当我单击eGuide时,我希望侧边栏显示其他Eguide(最多2(,而没有其他内容。为此,我有以下查询:

global $post;
$args = array(
    'post_type' => 'resources',
    'category__in'   => wp_get_post_categories($post->ID ),
    'posts_per_page' => 3,
    'post__not_in'   => array($post->ID )
);
$relatedPosts = new WP_Query( $args );

但是它也显示article吗?我也尝试了:

'post__not_in'   => array(get_the_ID() )

...仍然没有运气。

global $post;
$term_list = wp_get_post_terms( $post->ID, 'your_taxonomy_here', array( "fields" => "ids", "parent" => 0 ) );
$args = array (
    'post_type' => 'resources',
    'posts_per_page' => 3,
    'post__not_in' => array( $post->ID ),
    'post_status' => 'publish',
    'tax_query' => array(
        array (
            'taxonomy' => 'your_taxonomy_here',
            'field' => 'term_id',
            'terms'    => $term_list[0],
        ),
    ),
);
$relatedPosts = new WP_Query( $args );

您可以尝试此代码以获取相关帖子。

最新更新