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() ) : 

<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 />


I am updating my complete answer based on your clarification in the comment below. I hope this helps:

<div class="entry-content">

   <h2>Documents written by this writer</h2>
         *  Query posts for a relationship value.
         *  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)

         $documents = get_posts(array(
                     'post_type' => 'document',
                     'meta_query' => array(
                            'key' => 'writer', // name of custom field
                            'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
                            'compare' => 'LIKE'

        <?php if( $documents ): ?>
             <?php foreach( $documents as $document ): ?>
                   <a href="<?php echo get_permalink( $document->ID ); ?>">
                     <?php echo get_the_title( $document->ID ); ?>
             <?php endforeach; ?>
      <?php endif; ?>

  • Hi ItsMePN, sorry for not explaining properly. The relationship field actually contains an array of "Writers" which are linked to Documents. So, I want to display a list of Documents containing the current Writer's name. Thank you. – Stanley Umeanozie Dec 31 '15 at 15:47
  • I have updated my answer and code. Please check. – Prasad Nevase Dec 31 '15 at 17:11
  • This was my solution as well. It's vitaly important to put the meta_query in an array INSIDE another array for it to work. Thanks! – Jos Faber Jun 19 '19 at 7:37
  • Thank you, saved me – Amino Jan 7 '20 at 12:06

If someone has problems with displaying more than 5 results, here's a small tweak.

$documents = get_posts(array(
                     'post_type' => 'document',
                     'posts_per_page' => -1, //add this line
  • Note that this may crash your server because it will pull all posts of that type. – fuxia Oct 25 '20 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.