目前,我只能通过特定网站上的用户角色获取帖子。我需要的是有一个查询,将获得来自其他网站上的用户角色的帖子。
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发布的帖子
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\' ) );
}