首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按用户角色在多个站点中获取帖子

问按用户角色在多个站点中获取帖子
EN

WordPress Development用户
提问于 2018-02-23 06:27:52
回答 2查看 104关注 0票数 0

目前,我只能通过特定网站上的用户角色获得帖子。我需要的是有一个查询,将从其他网站的用户角色的帖子。

代码语言:javascript
复制
Example Users:
Site A - user(admin site a, editor site a)
Site B - user(editor site b)
Site C - user(editor site c)

网站A-职位索引清单

  • 由管理站点A发布
  • 按编辑网站A张贴

B站点-职位索引清单

  • 由管理站点A发布
  • 按编辑站点B发布。

站点C-职位索引列表

  • 由管理站点A发布
  • 按编辑网站C发布。
EN

回答 2

WordPress Development用户

发布于 2018-02-23 06:46:32

代码语言:javascript
复制
global $switched;
switch_to_blog(2); //switched to your site id based on which site post need.

$administrators = get_users( array( 'role' => 'administrator' ) ); // you can add more role on same query add with "," separated.
$administrators_ids = array();

$original_blog_id = get_current_blog_id(); 


foreach( $administrators as $admin ):
    $administrators_ids[] = $admin->ID;
    $news = new WP_Query( array( 'author' => implode( ',', $administrators_ids ), 'post_type' => 'news', 'paged' => get_query_var('paged') ) );
endforeach;
    if ( $news->have_posts() ) : 
        while ( $news->have_posts() ) : $news->the_post();

            //add code here

        endwhile; 

    endif;
wp_reset_postdata();
restore_current_blog();
票数 0
EN

WordPress Development用户

发布于 2018-02-23 11:30:04

试试这个:

代码语言:javascript
复制
 //admin posts from main site:
    switch_to_blog(get_main_site_id());
 $admin_posts =   get_posts_by_user_roles(array('role'=>'administrator'));
    restore_current_blog();

    //editor posts from current blog
 $editor_posts =   get_posts_by_user_roles();
 echo "";
 print_r($admin_posts);
 echo "
";
 echo "
";
 echo "
";
 print_r($editor_posts);
 exit;

function get_posts_by_user_roles($args = array()) {
     $args = wp_parse_args( $args,  array( 'role' => 'editor', 'fields'=>"ID" ) );
       $users = get_users($args);
       if(empty($users))
       {
           echo "No users found";//print for debug purpose only
           return FALSE;
       }
       return get_posts( array('author' => implode(',', $users), 'post_type' => 'post' ) );

}
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/294907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档