首页 > 解决方案 > 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() )

......仍然没有运气。

标签: phpwordpress

解决方案


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 );

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


推荐阅读