0
votes

I have a custom post_type 'publications' and it has a Repeater field 'linked_author' with a sub-field 'paper_linked_author' - a post object.

Here is the code to get all of the publications post and run a loop to get all authors linked to each 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//
                        }
                    }

            }
        }

I have been able to get all the sub-field values of each publication post however I'm not able to echo details of each linked author - the sub-field values. A print_r($artist_repeater) shows below details for one of the posts.

So, my question is how to list all unique authors from below print_r() array? Also, I need to link each author name such that clicking will bring list of all publications post he/she is linked to? Please help!

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
            )

    )

 )
1

1 Answers

0
votes

Just so it helps others, I solved the above problem with below code.

Basically, I was looking to list all authors who have written at least one Publication. So, earlier I was querying publications custom post_type and then looping each publication to list linked authors and that is where the above print_r() dump can be seen.

So, instead I queries all authors (a custom post_type) and then queries publications to match the key "linked_author_$_paper_linked_author" with the ID of the author post. It worked.

<?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; 
?>