Display List Of Posts Containing a Relationship Field Value [ACF]

Question

I have a post type called ‘Writers’ and another post type called ‘Documents’.

I use the Relationship field in Advanced Custom Fields to link a list of Writers (post objects) to a Document. This means that the field is basically an array containing multiple writers (some contain only one).

I have a template in my theme where I display additional information for each Writer inside their individual posts. I would like to display a list of documents they have contributed to (i.e. Posts containing the writer’s name in the relationship field).

I have tried the following query but it doesn’t work:

$item        = 0;
$writerName = get_the_title();

$my_contributions = new WP_Query( array( 
    'post_type'         => 'documents',
    'posts_per_page'    => -1,
    'meta_key'          => 'doc_contributors',
    'meta_value'        => $writerName,
    'meta_compare'      => 'LIKE'
) );

if( $my_contributions->have_posts() ) : 
    while( $my_contributions->have_posts() ) : 
        $my_contributions->the_post();
        $item++;
?>

<div class="list-line margint10 clearfix">
    <?php echo esc_attr( $item ) . ". " ?><a href="<?php the_permalink(); ?>"><?php get_the_title( $my_contributions->ID ); ?></a>
    <br />
</div>

<?php
    endwhile;
endif;
wp_reset_query();
0
Stanley Umeanozie 2 years 2020-10-25T03:10:19-05:00 0 Answers 11 views 0

Leave an answer

Browse
Browse