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

时间:2018-02-23 作者:jack

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

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发布的帖子-帖子索引列表

管理员站点A发布的帖子编辑站点B发布的帖子。站点C-帖子索引列表

管理员站点A发布的帖子编辑站点C发布的帖子

2 个回复
SO网友:Ravi Patel
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();
SO网友:Faiyaz Alam

Try this:

 //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 "<pre>";
 print_r($admin_posts);
 echo "<br>";
 echo "<br>";
 echo "<br>";
 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\' ) );

}
结束

相关推荐

WordPress MultiSite Theme

我有多个网站。(3个网站)所以我想在其中一个网站上应用不同的主题。但当我激活主题时。所有站点都受到影响。这就是多站点的工作方式吗??有人能澄清一下多站点可以做什么吗?安装多站点(子目录)修改了wp配置。php和。htaccessdefine( \'WP_ALLOW_MULTISITE\', true ); define(\'MULTISITE\', true); define(\'SUBDOMAIN_INSTALL\', false); define(\'DOMAIN_CURREN