ACF relationship fields – get_field values from other post type

In my POSTS page (regular post type), I have setup a ACF relationship field. Inside this I can select company name which are all under the post type of directory_listings.

Now, I have the following code on the directory listings page, and therefore using simply get_field does not work because those values are not on this page, they are elsewhere on the POST type instead.

Read More

So unsure how to grab the information.

Code on one of the pages which is under the DIRECTORY_LISTINGS post type:

$posts = get_field('related_articles');

if( $posts ): ?>
    <ul>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endforeach; ?>
    </ul>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

Example diagram as I am not great with explaining via text.
enter image description here

Currently I have setup the relationship field on the company edit page (directory_listing) instead. It works when doing the following:
1) Related Posts to this business listing -> select a post -> publish – > now displays the list on the business listing page. Example here: http://bit.ly/1vwydDl (bottom of the page)

2) I’d like from the POST edit page select a business which posts will appear on. I can put the field there via ACF no problem but getting it to actually display the results I cannot figure out.

Related posts

Leave a Reply

3 comments

  1. Background Info:

    get_field() has three parameters:

    1. $field_name: the name of the field to be retrieved. eg “page_content” (required)
    2. $post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc
    3. $format_value

    If you were only concerned with grabbing a specific post (of which you knew the ID), the key would be the second parameter ($post_id). There’s nothing really magical about ACF. Quite simply: the meta_value (i.e. the directory listing the post is tied to) is saved to each post (attached to that post’s $post_id).

    However, in your case, we don’t know the ID of the post(s) we want to get.

    Solution:

    If we explain what you want to do in a simple sentence, that sentence would look something like:

    Show/get posts on a directory_listings (custom post type) page which have a meta_value which points to that page.

    Obviously, you can’t use get_field(), because your problem has nothing to do with “getting a field.” Rather, you need to “find the posts that have a specific field.” ACF has great documentation on this.

    Luckily for you, WordPress ships with an awesome class called a WP_Query, and a similarly awesome function called get_posts(). So looking back at our sentence above and translating it into a function, we want to: get_posts() where the meta_key has a value of the current $post_id.

    Or, more specifically, on your directory_listings page, you’d have the following query:

    $related_articles = get_posts(array(
        'post_type' => 'post',
        'meta_query' => array(
            array(
                'key' => 'related_articles', // name of custom field
                'value' => '"' . get_the_ID() . '"',
                'compare' => 'LIKE'
            )
        )
    ));
    
    if( $related_articles ): 
        foreach( $related_articles as $article ): 
    
        // Do something to display the articles. Each article is a WP_Post object.
        // Example:
    
        echo $article->post_title;  // The post title
        echo $article->post_excerpt;  // The excerpt
        echo get_the_post_thumbnail( $article->ID );  // The thumbnail
    
        endforeach;
    endif;
    
  2. If it’s a custom field you are looking for within that query then you can do it like this:

    <?php
    $posts = get_field('related_articles');
    
    if( $posts ): ?>
        <ul>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <?php the_field('your_custom_field',$post); ?>
            </li>
        <?php endforeach; ?>
        </ul>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    
  3. If I understood this correctly this is the same problem than the one I had.
    In my case I preferred working with shortcodes.
    Using [acf field="person"] with the field being a relationshipt only gave me the ID of the person, but I was unable to acces their fields.
    On the other hand, using [acf field="last_name", post_id=[acf field="person"], which would be an ideal solution does not work as the wordpress parser does not allow nested shortcodes.

    that is why I cam up with this very small php solution:

    function import_cf_from_cpt( $atts ) {
        // import custom fields from other custom post types that are connected via a relationship to the calling custom post type
        // shortcode: [import <field from calling CPT of type relationship> <field name from field from other CPT>]
        // ex [import person last_name] --> Doe
    
        $postID = do_shortcode("[acf field={$atts[0]}]");
        // we use the first attribute to get the ID of the object
        $postField = get_field($atts[1], $postID);
        // next, using the ID we look for the field of the second attribute.
    return $postField;
    }
    add_shortcode( 'import', 'import_cf_from_cpt' );
    

    Placing this in the functions.php file allows to use the shortcode

    [import person last_name] for example, or any other combination to import field values from other posts.