目前,我只能通过特定网站上的用户角色获得帖子。我需要的是有一个查询,将从其他网站的用户角色的帖子。
Example Users:
Site A - user(admin site a, editor site a)
Site B - user(editor site b)
Site C - user(editor site c)网站A-职位索引清单
B站点-职位索引清单
站点C-职位索引列表
发布于 2018-02-23 06:46:32
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();发布于 2018-02-23 11:30:04
试试这个:
//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' ) );
}https://wordpress.stackexchange.com/questions/294907
复制相似问题