首页 > 解决方案 > 显示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
            )

    )

 )

标签: repeateradvanced-custom-fields

解决方案


只是为了帮助其他人,我用下面的代码解决了上述问题。

基本上,我希望列出所有至少写过一篇出版物的作者。所以,早些时候我查询出版物自定义 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; 
?>

推荐阅读