显示来自多个用户角色的帖子

时间:2014-02-02 作者:Kylie Lakevold Cawte

我创建了一个自定义循环,只显示管理员帖子,但现在我希望它同时显示管理员和贡献者的帖子。

非常感谢您的帮助!

以下是我目前掌握的情况:

echo "<div class=\'post_wrap " . $infinite . "\'>";
$users_query = new WP_User_Query( array( 
                                        \'role\' =>  \'administrator\',

                                        \'orderby\' => \'display_name\'
                                        ) );
$site_admins = array();
$results = $users_query->get_results();
foreach($results as $user) {
    $site_admins[] = $user->ID;
}

$admins = implode(\',\',$site_admins);
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; 
query_posts(array(\'post_type\' => \'post\', \'author\' => $admins, \'paged\' => $paged, \'posts_per_page\' => 10));
while (have_posts()) : the_post();

1 个回复
最合适的回答,由SO网友:M Haseeb 整理而成

// Query user data on admins
$administrators = new WP_User_Query( array( \'role\' =>  \'administrator\' ) );

// Query user data on contributors
$contributors = new WP_User_Query( array( \'role\' =>  \'contributor\' ) );

// Save admin IDs
foreach( $administrators->results as $u )
    $user_ids[] = $u->ID;

// Save contributor IDs
foreach( $contributors->results as $u )
    $user_ids[] = $u->ID;

// Little cleanup
unset( $u, $administrators, $contributors );

// Save paged value (for paginating results)
$paged = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1; 

// query_posts()
query_posts(
    array( 
        \'post_type\' => \'post\',
        \'author__in\' => $user_ids,
        \'paged\' => $paged
    )
);

// If the query has data
if( have_posts() ) :

    // Post loop
    while ( have_posts() ) : 

        // Setup post data
        the_post();
        ?>

        <!-- Do HTML markup and template tags here, eg. the_content(), the_title() etc.. -->
        <h1><?php the_title(); ?></h1>

        <?php
    endwhile;

// End "If the query has data"
endif;
@1fixdotio:WP_User_Query 返回每个用户的对象。WP_Query 只获取用户ID,因此我们必须从用户对象中提取ID。我认为这是唯一的错误。

结束

相关推荐

Pagination on Custom Loop

我想将最新帖子的样式与自定义页面上某个类别中的所有其他帖子的样式不同。到目前为止,我有下面的代码,它正是这样做的:<?php if (have_posts()) : ?> <?php query_posts(\"cat=4&showposts=1\"); ?> <?php while (have_posts()) : the_post(); ?> (Style1)<?php the_title(); ?><