12. Relational Fields (Relationship, Post Object, Taxonomy) | Field Forge - Custom Fields, Built for Speed
Download Log in

12. Relational Fields (Relationship, Post Object, Taxonomy)

Developer Guide

Fields that create connections between content. Requires PRO license.

Relationship

Multi-select post picker that returns an array of posts.

php
$related = get_field( 'related_articles' );
// return_format = 'object': Returns array of WP_Post objects
// return_format = 'id': Returns array of post IDs, e.g., [10, 25, 33]

if ( $related ) :
    echo '<div class="related-articles">';
    echo '<h3>Related Articles</h3>';
    foreach ( $related as $post ) : setup_postdata( $post ); ?>
        <article class="related-card">
            <?php if ( has_post_thumbnail() ) : ?>
                <?php the_post_thumbnail( 'medium' ); ?>
            <?php endif; ?>
            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
            <?php the_excerpt(); ?>
        </article>
    <?php endforeach;
    wp_reset_postdata();
    echo '</div>';
endif;

// When return_format = 'id', use get_posts for more control
$related_ids = get_field( 'related_articles' );
if ( $related_ids ) {
    $posts = get_posts( [
        'post__in'       => $related_ids,
        'orderby'        => 'post__in',
        'posts_per_page' => -1,
    ] );
    foreach ( $posts as $p ) {
        echo '<a href="' . get_permalink( $p ) . '">' . esc_html( $p->post_title ) . '</a>';
    }
}

Post Object

Select one or multiple posts.

php
$featured = get_field( 'featured_post' );
// return_format = 'object': Returns WP_Post object (or array if multiple)

if ( $featured ) {
    echo '<a href="' . get_permalink( $featured ) . '">';
    echo esc_html( $featured->post_title );
    echo '</a>';
}

// Multiple post objects
$highlights = get_field( 'highlighted_posts' );
if ( $highlights ) {
    foreach ( $highlights as $post ) {
        echo '<div class="highlight">';
        echo '<h4>' . esc_html( $post->post_title ) . '</h4>';
        echo '</div>';
    }
}

Page Link

Returns the permalink URL of a selected post/page.

php
$link = get_field( 'destination_page' );
// Returns: URL string (permalink of selected page)
echo '<a href="' . esc_url( $link ) . '" class="btn">Go to page</a>';

Taxonomy

Select taxonomy terms.

php
$term = get_field( 'primary_category' );
// return_format = 'object': Returns WP_Term object

if ( is_object( $term ) ) {
    echo '<a href="' . esc_url( get_term_link( $term ) ) . '">' . esc_html( $term->name ) . '</a>';
}

// Multiple terms
$tags = get_field( 'related_tags' );
if ( $tags ) {
    echo '<div class="tags">';
    foreach ( $tags as $tag ) {
        echo '<a href="' . esc_url( get_term_link( $tag ) ) . '" class="tag">' . esc_html( $tag->name ) . '</a>';
    }
    echo '</div>';
}

User

Select one or more WordPress users.

php
$author = get_field( 'guest_author' );
// return_format = 'array': Returns ['ID' => 5, 'display_name' => 'John', 'user_email' => '...']

if ( $author ) {
    echo '<div class="author-card">';
    echo get_avatar( $author['ID'], 64 );
    echo '<span>' . esc_html( $author['display_name'] ) . '</span>';
    echo '</div>';
}

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs