【问题标题】:List User by use role with count of posts they are assigned to按使用角色列出用户以及分配给他们的帖子数
【发布时间】:2023-01-26 22:34:10
【问题描述】:

我想按角色显示用户列表,其中包含分配给他们的帖子数量。我使用 ACF 用户字段将帖子分配给用户,这是我目前所做的,不确定我是否朝着正确的方向前进。

$args_user_role = array(
    'role'    => 'um_pds-project-manager',
    'orderby' => 'user_nicename',
    'order'   => 'ASC'
);
$users = get_users( $args_user_role );

$args_projects = array(
  'posts_per_page' => -1,
  'post_type' => 'project',
  'meta_query' => array(
            'relation' => 'AND',
    array(
        'key'   => 'status',
        'value' => '1'
    ),
          array(
        'key' => 'pds_project_manager',
        'value' => $users,
        'compare' => 'LIKE'
    )
  )
);
$posts = get_posts($args_projects);
$pm_count = count($posts);//this is the total number of posts

【问题讨论】:

  • 我想尝试帮助,但可能需要更多信息。您使用什么类型的字段来分配职位?用户配置文件中带有帖子对象或 ID 的转发器?如果您只是想列出用户,我只是不太确定为什么要使用 get_posts。
  • 每个帖子都有一个用 ACF 创建的用户字段,用户字段返回一个用户数组。该字段允许选择多个用户。 advancedcustomfields.com/resources/user 我使用这个字段来控制我的哪些用户可以看到哪些帖子,并且需要提供一个计数来显示每个用户分配了多少帖子(出现在该用户字段中)。

标签: php wordpress advanced-custom-fields acfpro


【解决方案1】:

这可能对你有用。我有一个转发器字段设置如下:

并将子字段的返回值设置为用户数组:

然后遍历帖子和转发器,并将用户名、帖子 ID、计数和任何您喜欢的其他内容添加到数组 ($user_arr) 中:

foreach($posts as $post):
    if ( have_rows('user_repeater') ) :
        while( have_rows('user_repeater') ) : the_row();
            $user_arr = get_sub_field('user');
            $arr[$user_arr['user_nicename']]['post_ids'][] =  $post->ID;
            $arr[$user_arr['user_nicename']]['post_count']++;
        endwhile;
    endif;
endforeach;

我的数组看起来像这样,您可以使用它来创建您的列表:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-04
    • 2015-08-01
    • 2012-09-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    相关资源
    最近更新 更多