Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WordPress-为每个用户获取帖子_Wordpress_Advanced Custom Fields_Timber - Fatal编程技术网

WordPress-为每个用户获取帖子

WordPress-为每个用户获取帖子,wordpress,advanced-custom-fields,timber,Wordpress,Advanced Custom Fields,Timber,对于学生的项目,我使用WordPress和木材(树枝)+ACF 在这个项目中,我创建了3种自定义帖子类型:论文,主题和主题自由 每个学生只能按自定义帖子类型创建一篇帖子(我为此创建了一个限制) 但是现在我想显示一个列表,上面有每个学生的名字和他们的3篇文章 这样的清单: Nicolas Mapple 自定义帖子类型标题论文+自定义帖子类型名称+图片(ACF字段) 自定义帖子类型标题主题+自定义帖子类型名称+图片(ACF字段) 自定义帖子类型标题主题自由+自定义帖子类型名称+图片(ACF字段)

对于学生的项目,我使用WordPress和木材(树枝)+ACF

在这个项目中,我创建了3种自定义帖子类型:
论文
主题
主题自由

每个学生只能按自定义帖子类型创建一篇帖子(我为此创建了一个限制)

但是现在我想显示一个列表,上面有每个学生的名字和他们的3篇文章

这样的清单:

Nicolas Mapple

  • 自定义帖子类型标题
    论文
    +自定义帖子类型名称+图片(ACF字段)
  • 自定义帖子类型标题
    主题
    +自定义帖子类型名称+图片(ACF字段)
  • 自定义帖子类型标题
    主题自由
    +自定义帖子类型名称+图片(ACF字段)
Brenda Smith

  • 自定义帖子类型标题
    论文
    +自定义帖子类型名称+图片(ACF字段)
  • 自定义帖子类型标题
    主题
    +自定义帖子类型名称+图片(ACF字段)
  • 自定义帖子类型标题
    主题自由
    +自定义帖子类型名称+图片(ACF字段)
首先,我尝试获取每个学生的ID:

$students = get_users( array(
    'role'    => 'student',
    'orderby' => 'user_nicename',
    'order'   => 'ASC',
    'has_published_posts' => true
));

$students_id = array();

foreach ($students as $student) {
    $students_id[] = $student->ID;
}
之后,从以下ID获取所有帖子:

$get_posts_students = get_posts( array(
    'author' => $students_id,
    'post_type' => array('dissertation', 'subject-imposed', 'subject-free')
));

$context['list_of_students'] = $get_posts_students;
我得到了一个错误
urldecode(),它希望参数1是字符串和一个数组,但包含所有帖子,而不是按学生分组

能帮我点忙吗?如何按学生分组帖子

更新-更好但不完整(由@disifor提供的解决方案): 使用foreach中的
get_posts
,我将posts分组。但是我没有每组学生的名字

$students = get_users( array(
    'role'    => 'student',
    'orderby' => 'rand',
    'has_published_posts' => true
));

$students_posts = array();

foreach ($students as $student) {
    $students_posts[] = get_posts( array(
        'author' => $student->ID,
        'post_type' => array('dissertation', 'subject-imposed', 'subject-free')
    ));
}

$context['students_posts'] = $students_posts;
我得到了一个这样的数组,我想要每组学生的名字:

您可以使用
get\u user\u meta()
将学生的姓名包含到数组中:


您可以使用
get\u user\u meta()
将学生的姓名包含到数组中:


为什么不将您的
get_posts
移动到
foreach
中,而不是使用它来用id填充数组?您仍然可以使用数组,但可以使用它来保存每个学生的所有帖子。这样他们就分组了。谢谢。我不知道这是可能的。我试过了。这就是工作。但我没有每组学生的名字。仅帖子组我更新了我的帖子为什么不将你的
get_posts
移动到
foreach
中,而不是使用它来用ID填充数组?您仍然可以使用数组,但可以使用它来保存每个学生的所有帖子。这样他们就分组了。谢谢。我不知道这是可能的。我试过了。这就是工作。但我没有每组学生的名字。我更新了我的帖子谢谢你的帮助。我只有一个名字。这只有一个名称在每组帖子的数组之外,而不是在每个组内。我认为您需要更改
$context
——因为您试图使用该名称返回帖子,但您需要将它们分组。因此,您可能需要将
$context
放在foreach中。我没有树枝/木材来测试。谢谢你的帮助。我只有一个名字。这只有一个名称在每组帖子的数组之外,而不是在每个组内。我认为您需要更改
$context
——因为您试图使用该名称返回帖子,但您需要将它们分组。因此,您可能需要将
$context
放在foreach中。我没有树枝/木材来测试这一点。
foreach ($students as $student) {
    // Get the first name from user meta.
    $first_name = get_user_meta( $student->ID, 'first_name');
    // Get the last name from user meta.
    $last_name = get_user_meta( $student->ID, 'last_name');
    // Add a new array key for "name". The first name and last name return as arrays, so we use [0] to get the first index.
    $students_posts['name'] = $first_name[0] . ' ' . $last_name[0];
    $students_posts[] = get_posts( array(
        'author' => $student->ID,
        'post_type' => array('dissertation', 'subject-imposed', 'subject-free')
    ));
}

$context['students_posts'] = $students_posts;