repeater - 显示Repeater子字段,一个post对象值
问题描述
我有一个自定义 post_type 'publications',它有一个 Repeater 字段 'linked_author' 和一个子字段 'paper_linked_author' - 一个帖子对象。
这是获取所有出版物帖子并运行循环以获取所有作者链接到每个帖子的代码。
$args = array(
'posts_per_page' => '-1',
'post_type' => 'publications'
);
$the_query = new WP_Query( $args );
$post_ids = wp_list_pluck( $the_query->posts, 'ID' );
echo "<pre>";
print_r($post_ids);
echo "</pre>";
$artist_list = array();
foreach($post_ids as $post_id){
$artist_repeater = get_field('linked_author', $post_id);
echo "<pre>";
print_r($artist_repeater);
echo "</pre>";
if ($artist_repeater) {
$speakers = get_sub_field('paper_linked_author');
if ($speakers && count($speakers)>0)
{
foreach ($speakers as $speaker)
{
echo $speaker->ID; //$speaker is a post object//
}
}
}
}
我已经能够获取每个发布帖子的所有子字段值,但是我无法回显每个链接作者的详细信息 - 子字段值。print_r($artist_repeater) 下面显示了其中一篇文章的详细信息。
所以,我的问题是如何列出 print_r() 数组下方的所有唯一作者?另外,我需要链接每个作者姓名,以便单击将显示他/她链接到的所有出版物列表?请帮忙!
Array
(
[0] => Array
(
[paper_linked_author] => WP_Post Object
(
[ID] => 4885
[post_author] => 1
[post_date] => 2018-06-26 04:56:29
[post_date_gmt] => 2018-06-26 04:56:29
[post_content] =>
[post_title] => Q1-Test
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => q1-test
[to_ping] =>
[pinged] =>
[post_modified] => 2018-06-26 05:14:13
[post_modified_gmt] => 2018-06-26 05:14:13
[post_content_filtered] =>
[post_parent] => 0
[menu_order] => 0
[post_type] => people
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
[1] => Array
(
[paper_linked_author] => WP_Post Object
(
[ID] => 2115
[post_author] => 1
[post_date] => 2017-03-28 05:47:01
[post_date_gmt] => 2017-03-28 05:47:01
[post_content] =>
[post_title] => Julius Ceaser
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => julius-ceaser
[to_ping] =>
[pinged] =>
[post_modified] => 2018-06-25 11:02:55
[post_modified_gmt] => 2018-06-25 11:02:55
[post_content_filtered] =>
[post_parent] => 0
[menu_order] => 0
[post_type] => people
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
)
解决方案
只是为了帮助其他人,我用下面的代码解决了上述问题。
基本上,我希望列出所有至少写过一篇出版物的作者。所以,早些时候我查询出版物自定义 post_type,然后循环每个出版物以列出链接的作者,这就是上面的 print_r() 转储可以看到的地方。
因此,我查询所有作者(自定义 post_type),然后查询出版物以将键“linked_author_$_paper_linked_author”与作者帖子的 ID 匹配。有效。
<?php
add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
$args = array(
'post_type' => 'people',
'posts_per_page' => -1, // Unlimited posts
'post_status' => 'publish'
// Order alphabetically by name
);
$loops = new WP_Query( $args );
while ($loops -> have_posts()) : $loops -> the_post();
$args = array(
'numberposts' => -1,
'post_type' => 'publications',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'linked_author_$_paper_linked_author',
'compare' => '=',
'value' => get_the_ID()
)
)
);
// query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
?>
<ul>
<li><a href="<?php echo get_the_permalink();?>"><?php echo the_title(); ?></a></li>
</ul>
<?php
endif;
endwhile;
?>
推荐阅读
- c# - 还原 VS 2019 16.5.0 后找不到 Nuget 包
- android - 主要活动开始活动与我的 android 项目的两个模块
- tensorflow.js - 何时返回张量而不是 Blazeface 中的值
- python - CNN模型预测一类,准确率99%
- r - 主成分回归?什么是因变量?
- javascript - Vue 组件在包装到另一个组件时停止工作
- node.js - FacebookTokenError:无法加载 URL:此 URL 的域不包含在应用程序的域中
- java - Java Node.setTextContent((myString)) 将 < 和 > 转换为 < 和 >
- javascript - 更改 JS 中的输入,然后编辑 php 文件
- azure - 如果用户不是参与者或所有者,Azure 用户如何创建资源组?