2. get_field() and the_field() | Field Forge - Custom Fields, Built for Speed
Download Log in

2. get_field() and the_field()

Developer Guide

The two most fundamental functions in Field Forge. get_field() retrieves a value; the_field() echoes it directly.

get_field( $selector, $post_id )

Retrieve a single field value.

Parameters:
ParameterTypeDefaultDescription
$selectorstringField name (e.g., 'subtitle') or field key (e.g., 'field_abc123')
$post_idintstringfalsefalsePost ID, 'options', 'user_123', 'term_45', 'comment_67', or false for current post in The Loop
Returns: mixed — the field value formatted according to the field’s return_format setting. Returns null if not found. Post ID Format Reference:
FormatExampleTarget
falseget_field('title')Current post (requires The Loop)
intget_field('title', 42)Specific post by ID
'options'get_field('logo', 'options')Options page (default slug)
'user_N'get_field('bio', 'user_5')User meta for user ID 5
'term_N'get_field('color', 'term_12')Term meta for term ID 12
'comment_N'get_field('rating', 'comment_99')Comment meta for comment ID 99
php
// Current post (inside The Loop)
$title = get_field( 'custom_title' );

// Specific post
$price = get_field( 'product_price', 42 );

// Options page
$logo = get_field( 'site_logo', 'options' );

// User meta
$bio = get_field( 'biography', 'user_' . get_current_user_id() );

// Term meta
$category_color = get_field( 'brand_color', 'term_' . $term_id );
Return format examples by field type:
php
// Image field with return_format = 'array' (default)
$image = get_field( 'hero_image', 123 );
// Returns: ['ID' => 55, 'url' => '...', 'width' => 1200, 'height' => 630, 'alt' => '...', 'title' => '...']

// Image field with return_format = 'url'
$image_url = get_field( 'hero_image', 123 );
// Returns: 'https://example.com/wp-content/uploads/2024/hero.jpg'

// Image field with return_format = 'id'
$image_id = get_field( 'hero_image', 123 );
// Returns: 55

// Select field with return_format = 'value'
$status = get_field( 'order_status', 200 );
// Returns: 'processing'

// Select field with return_format = 'label'
$status_label = get_field( 'order_status', 200 );
// Returns: 'Processing'

// Select field with return_format = 'array'
$status_full = get_field( 'order_status', 200 );
// Returns: ['value' => 'processing', 'label' => 'Processing']

// True/false field always returns bool
$is_featured = get_field( 'is_featured', 123 );
// Returns: true or false

// Relationship field with return_format = 'object'
$related = get_field( 'related_posts', 42 );
// Returns: [WP_Post, WP_Post, WP_Post]

// Repeater field returns array of row arrays
$rows = get_field( 'team_members', 42 );
// Returns: [['name' => 'Alice', 'role' => 'Dev'], ['name' => 'Bob', 'role' => 'Design']]

the_field( $selector, $post_id )

Echo a field value. Equivalent to echo get_field(...).

Parameters:
ParameterTypeDefaultDescription
$selectorstringField name or field key
$post_idintstringfalsefalseSame as get_field()
Returns: void (outputs directly).
php
<!-- Current post -->
<h1><?php the_field( 'page_heading' ); ?></h1>

<!-- Link with field values -->
<a href="<?php the_field( 'cta_link' ); ?>">
    <?php the_field( 'cta_text' ); ?>
</a>

<!-- Options page value -->
<footer>
    <p><?php the_field( 'footer_copyright', 'options' ); ?></p>
</footer>

<!-- User profile -->
<div class="author-bio">
    <p><?php the_field( 'biography', 'user_' . get_the_author_meta( 'ID' ) ); ?></p>
</div>

<!-- Specific post -->
<div class="promo-banner">
    <h3><?php the_field( 'promo_headline', 99 ); ?></h3>
    <p><?php the_field( 'promo_description', 99 ); ?></p>
</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