Advanced Custom Fields: getting started
Advanced Custom Fields (ACF) is one of the most popular WordPress plugins. It adds fields to the edit screens of posts, pages, users, terms and options pages through a visual interface, and gives a simple API to read the values in templates. Instead of the bare “Custom Fields” box with free text, editors get date pickers, image selectors, relationships and other proper inputs.
Problem: The built-in custom fields box offers only a key and a text value; there is no way to give editors an image field, a date picker or a list of related posts.
Solution: Install ACF, create a field group with location rules (post type, page template, taxonomy), and read the values with get_field(). For themes in version control, register the field groups in PHP.
Installation and the first field group
wp plugin install advanced-custom-fields --activate
Create a group in Custom Fields → Add New, add fields, and set the location, for example “Post Type is equal to Page”. The fields then appear on the edit screen of every page.
The free version has text, number, image, file, link, select, date picker, relationship and other basic types. Repeater, flexible content, gallery and options pages are part of ACF PRO.
Reading the values
<?php
// A text field of the current post
$subtitle = get_field( 'subtitle' );
if ( $subtitle ) {
echo '<p class="subtitle">' . esc_html( $subtitle ) . '</p>';
}
// An image field with "Return format: Image array"
$image = get_field( 'hero_image' );
if ( $image ) {
echo wp_get_attachment_image( $image['ID'], 'large' ); // srcset, width, height and alt from the media library
}
// A relationship field returns an array of WP_Post objects
$related = get_field( 'related_posts' );
if ( $related ) {
foreach ( $related as $related_post ) {
printf( '<a href="%s">%s</a>', esc_url( get_permalink( $related_post ) ), esc_html( get_the_title( $related_post ) ) );
}
}
get_field( 'subtitle', 42 ) reads the field of another post; how to get fields of other pages, terms and options is shown in getting an ACF field on different pages. Do not name the loop variable $post: that overwrites the global post of the template.
A repeater (ACF PRO)
<?php
if ( have_rows( 'faq_items' ) ) {
echo '<dl class="faq">';
while ( have_rows( 'faq_items' ) ) {
the_row();
printf( '<dt>%s</dt><dd>%s</dd>', esc_html( get_sub_field( 'question' ) ), wp_kses_post( get_sub_field( 'answer' ) ) );
}
echo '</dl>';
}
Field groups in code
Groups created in the admin live in the database. To keep them in Git together with the theme, register them in PHP:
<?php
add_action( 'acf/init', function () {
acf_add_local_field_group( array(
'key' => 'group_hero',
'title' => 'Hero section',
'fields' => array(
array(
'key' => 'field_hero_subtitle',
'label' => 'Subtitle',
'name' => 'subtitle',
'type' => 'text',
),
),
'location' => array(
array(
array( 'param' => 'post_type', 'operator' => '==', 'value' => 'page' ),
),
),
) );
} );
The keys (group_…, field_…) must be unique. ACF can also export existing groups as this PHP code (Tools → Export), or save them automatically as JSON files in an acf-json folder of the theme.
NOTE: ACF stores values as ordinary post meta, and the field key in a second meta row with an underscore (_subtitle). When you move a site or copy content with SQL, copy both rows, otherwise get_field() returns raw values. A typical case is described in ACF link field values lost after a migration.