我有一个自定义的post_type'publications',它有一个Repeater字段'linked_author'和一个子字段'paper_linked_author'-一个post对象。
这里是获取所有出版物的代码,并运行循环以使所有作者链接到每个帖子。
$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
)
)
)
答案 0 :(得分:0)
只是对其他人有帮助,所以我用下面的代码解决了上述问题。
基本上,我希望列出所有写至少一篇出版物的作者。因此,之前我查询的是出版物的自定义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;
?>