WordPress-仅显示与登录用户相关的帖子

时间:2020-01-08 15:34:07

标签: php wordpress podscms

在我的WordPress网站中,我使用PODS插件为Tanks,Locations和Users创建了自定义帖子类型设置。当前,我有一些PHP允许我仅向登录的用户显示一个帖子(如果列出了该帖子的作者)。这是一个很好的开始,但并不完全是我需要它的工作方式。由于WordPress存储其“作者”字段的方式,每个罐/位置只能关联一个用户,而不是多个。我的问题是,我将如何处理它,以便WordPress检查当前登录用户与之关联的坦克/位置,并仅显示这些坦克/位置?在“储罐”和“位置” PODS中,我已经为“用户” POD设置了一个关系字段,这使我可以根据需要为每个“储罐/位置”分配尽可能多的用户。现在我需要弄清楚如何显示该信息。如果有什么不同,我所有PODS的存储类型都是基于表的。

1 个答案:

答案 0 :(得分:0)

因此,如果有人提出与我相同的问题,这就是我最终解决问题的方式。变量“ users.ID”是指我在每个“位置/储罐”中拥有的关系字段。

if(!current_user_can('administrator')){
add_shortcode( 'pods_by_current_user_cpt', 'pods_by_current_user_cpt' );

    function pods_by_current_user_cpt( $atts, $content = null ) {
        $current_user  = wp_get_current_user();
        $user_id       = $current_user->ID;
        $atts['where'] = 'users.ID = ' . (int) $user_id;
        return pods_shortcode( $atts, $content );
    }
}
else{
    add_shortcode( 'pods_by_current_user_cpt', 'pods_by_current_user_cpt' );

    function pods_by_current_user_cpt( $atts, $content = null ) {
        $current_user  = wp_get_current_user();
        $user_id       = $current_user->ID;
        return pods_shortcode( $atts, $content );
    }
}
相关问题