3

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();
6

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>
        <?php 
        /*
         *  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(
                      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 ): ?>
             <ul>
             <?php foreach( $documents as $document ): ?>
                <li>
                   <a href="<?php echo get_permalink( $document->ID ); ?>">
                     <?php echo get_the_title( $document->ID ); ?>
                   </a>
                </li>
             <?php endforeach; ?>
            </ul>
      <?php endif; ?>

</div>
  • 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! – JosFabre Jun 19 at 7:37

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.