【问题标题】:How to list all users of posted in wordpress如何列出在wordpress中发布的所有用户
【发布时间】:2016-05-06 21:41:07
【问题描述】:
$args  = array(
'orderby' => 'display_name'
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors))
{
    echo '<ul>';
    foreach ($authors as $author)
    {
        $author_info = get_userdata($author->ID);
        echo '<li>'.$author->ID.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
    }
    echo '</ul>';
} else {
    echo 'No authors found';
}

我正在使用帖子作者的帖子过滤器。上面的代码显示了所有用户,因此只需要显示在博客中发帖的作者。

就像 wp_list_authors() 函数,但我需要获取作者 ID 而不是作者姓名。因为我需要创建一个下拉列表。当有人更改选项时,我需要在 AJAX 中获取该作者的帖子

【问题讨论】:

  • 您的$post-&gt;post_author 中有什么内容?
  • 我不想通过获取所有帖子来获取作者。只需获取都在特定博客下发帖的作者,例如 wp_list_authors。
  • codex.wordpress.org/Function_Reference/wp_dropdown_users 你只能查询作者,除了循环所有帖子来构建你的 id,但这很耗费资源,缓存!

标签: wordpress list author


【解决方案1】:

更新 1:

希望得到帮助:

$posted = get_posts([
    'post_type' => 'your_custom_post_type',
]);
$author_ids = [];
foreach ($posted as $post) {
    $author_ids[] = $post->post_author;
}
$author_ids = array_unique($author_ids);
if (!empty($author_ids) )
{
    echo '<ul>';
    foreach ($author_ids as $user_id)
    {
        $author_info = get_userdata($user_id);
        echo '<li>'.$user_id.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
    }
    echo '</ul>';
} else {
    echo 'No authors found';
}

确保将post_type 更改为您的帖子类型,并且每个用户同时拥有first_namelast_name


Codex 参考文献中缺少一个参数:has_published_posts

$args = [
  'orderby' => 'display_name',
  'has_published_posts' => true
];
$authors = new WP_User_Query($args);
...

最好查找内部类文件,因为 Codex 上的信息并不总是最新的。

【讨论】:

  • 这听起来不错!我需要特定帖子类型的已发布帖子用户,但不是全部
  • @AbdulRahumanM 您需要更新您的问题。 已发帖的用户仅发帖的作者是什么意思?你的post_type是什么?
  • 显示发帖的用户(帖子类型=自定义帖子类型)
  • 感谢您的回答,它帮助了我:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 2014-08-08
  • 2019-10-18
  • 2016-01-21
  • 2015-12-11
  • 2019-01-25
相关资源
最近更新 更多