douzhang3898 2019-08-02 08:11
浏览 38
已采纳

WordPress,查询未找到相关帖子

I have three posts, two which have the tag eGuide one which has the tag Article. When I click on an eGuide, I want the sidebar to display other eGuide's (up to 2) and nothing else. To do this, I have the following query:

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

But it's showing the article too? I've also tried:

'post__not_in'   => array(get_the_ID() )

... still no luck.

  • 写回答

1条回答 默认 最新

  • doujianmin6527 2019-08-02 08:48
    关注
    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 );
    

    You can try this code to get related posts.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答