If you build WordPress themes with Advanced Custom Fields (ACF) PRO, here is how to display a Repeater field and query its rows. A Repeater lets editors add a list of similar items, such as team members or slides. The trouble starts when the rows show on the edit screen but the template prints nothing, or when you need every post where one row holds a certain value. Both get easier once you know that ACF stores a repeater in three parts: a row count under the field’s name, one row per value, and a hidden reference row tying each value to its field. Check those rows first, then loop with have_rows(), the_row() and get_sub_field().
What you’ll accomplish
- A Repeater field group for team members, with three sub fields and a clear location rule.
- Template code that prints every row, with the right post ID inside and outside The Loop (the WordPress loop that outputs the current page’s posts).
- A map of the
wp_postmetarows behind one repeater, read with WP-CLI. - A three-line check that tells you why a repeater prints nothing, and a script that repairs imported rows.
- A working query for posts where any row matches a value, and a sense of when the row count gets too large.
Prerequisites
- ACF PRO 6.x. The Repeater is a PRO field type; ACF’s site sells it as part of ACF PRO, and the free plugin from WordPress.org has no Repeater. Secure Custom Fields, the fork maintained by the WordPress.org team, does include one and stores it the same way.
- WordPress 5.3 or newer for the regular-expression query in step 7. Every other step works on any current version.
- WP-CLI (the
wpcommand), run over SSH as the site’s user from the folder that holdswp-config.php. - A theme you can edit, preferably a child theme, so an update to the parent theme doesn’t erase your template changes.
- If ACF itself is new to you, start with ACF field groups, location rules and get_field. This article picks up where that one stops.
How it works
A Repeater is a parent field that holds a set of sub fields. Each time an editor clicks Add Row, they get another copy of those sub fields. Every field, parent or sub field, has a field name you choose (team_members) and a field key that ACF generates once and never changes (field_6721c4e9a1b3f). The name becomes the database key. The key identifies the definition, which is what tells ACF the field’s type and how to format its value.
ACF saves a repeater as ordinary post meta, the key and value pairs in the wp_postmeta table. For post 214 with two team members, it writes these rows:
team_membersholds the number of rows, not the content. Here it is2.team_members_0_name,team_members_1_nameand so on hold the values. The pattern is the parent name, the row index starting at 0, then the sub field name.- Each value row has a partner that starts with an underscore, such as
_team_members_0_name. It holds the sub field’s key. _team_membersholds the repeater’s own key.
That last row is the one to watch. When you call have_rows( 'team_members' ), ACF reads _team_members to find the field definition. With the definition, it knows this is a repeater, reads the count, and builds the rows by name. Without it, ACF has no definition. In that case have_rows() returns false, and get_field() returns the raw count, the string '2', instead of an array. That is why imported content, written by a tool that copied only the visible meta keys, so often shows nothing.
A nested repeater follows the same pattern one level down. A skills repeater inside each team member is stored as team_members_0_skills (its row count), then team_members_0_skills_0_label, and so on.
What many rows cost
Each value costs two rows in wp_postmeta: the value and its reference. Three sub fields across 40 rows comes to 242 meta rows for one field on one post. WordPress loads all of a post’s meta in one query the first time anything asks for it, so a template doesn’t run one query per sub field. The cost shows up in other places. The edit screen sends every input on save, and large repeaters can pass PHP’s max_input_vars limit, covered in ACF fields not saving or returning empty. Queries that search inside rows also get slower as the table grows, which step 7 covers.
Step-by-step
- Confirm the Repeater field type is available. Run this before anything else, because a site with only the free plugin fails in confusing ways:
wp plugin list --fields=name,status,version --format=csv | grep -E 'advanced-custom-fields|secure-custom-fields'
wp eval 'echo acf_get_field_type( "repeater" ) ? "repeater: available\n" : "repeater: missing\n";'
Example output:
advanced-custom-fields-pro,active,6.4.2
repeater: available
repeater: missing means the active plugin is the free version. Install and activate ACF PRO. Activating it deactivates the free copy automatically and shows a notice saying so. The field groups and saved data stay in the database, so nothing is lost while you switch.
-
Build the field group. In the admin, go to ACF → Field Groups → Add New and name the group “Team”. Add one field:
- Field Type: Repeater. Field Label: Team members. Field Name:
team_members. - Under Sub Fields, add
name(Text),role(Text) andphoto(Image, with Return Format set to Image ID). - Set Layout to Table for short fields like these, and Button Label to “Add team member”.
Under Settings → Location Rules, choose Post Type is equal to Page, or a narrower rule such as one page template. Save the group, open page 214 (use your own page ID), add two rows and click Update. Then confirm ACF can resolve the field by name:
- Field Type: Repeater. Field Label: Team members. Field Name:
wp eval '$f = acf_get_field( "team_members" ); echo $f["key"], " ", $f["type"], " ", count( $f["sub_fields"] ), " sub fields\n";'
Example output:
field_6721c4e9a1b3f repeater 3 sub fields
Write that key down; step 6 uses it. A PHP error means ACF found no field with that name, so check that the group was saved. A type other than repeater means another field uses the same name. In both cases, copy the key from the field group’s screen instead. If the keys are hidden, open Screen Options at the top right of the field group editor and tick Field Keys.
- Read the rows ACF saved. List the post’s meta and keep only the repeater:
wp post meta list 214 --fields=meta_key,meta_value --format=csv | grep team_members
Example output:
team_members_0_name,"Ana Ruiz"
_team_members_0_name,field_6721c4f8a1b40
team_members_0_role,"Lead engineer"
_team_members_0_role,field_6721c503a1b41
team_members_0_photo,388
_team_members_0_photo,field_6721c50ea1b42
team_members_1_name,"Luc Gagnon"
_team_members_1_name,field_6721c4f8a1b40
team_members_1_role,Accountant
_team_members_1_role,field_6721c503a1b41
team_members_1_photo,402
_team_members_1_photo,field_6721c50ea1b42
team_members,2
_team_members,field_6721c4e9a1b3f
Every value row should have its underscore partner, and team_members should equal the number of rows you added. Notice that row 0 and row 1 point to the same sub field keys. The key belongs to the sub field definition, not to the row.
- Print the rows in your template. Add this to the page template, for example
page-team.phpin your child theme:
<?php
// $post_id: the page that holds the repeater. Inside The Loop, get_the_ID() is right.
$post_id = get_the_ID();
if ( have_rows( 'team_members', $post_id ) ) :
echo '<ul class="team">';
while ( have_rows( 'team_members', $post_id ) ) :
the_row(); // Moves to the next row. Without it the loop never ends.
$name = get_sub_field( 'name' );
$role = get_sub_field( 'role' );
$photo = get_sub_field( 'photo' ); // Image ID, from the Return Format setting.
echo '<li class="team-member">';
echo wp_get_attachment_image( $photo, 'thumbnail' );
echo '<strong>' . esc_html( $name ) . '</strong> ' . esc_html( $role );
echo '</li>';
endwhile;
echo '</ul>';
else :
echo '<p class="team-empty">No team members yet.</p>';
endif;
have_rows() checks whether a next row exists. the_row() moves to it. get_sub_field() reads one value from the current row only. Outside The Loop, such as in a header, footer or widget, replace get_the_ID() with get_queried_object_id() on a single page, or with the fixed ID of the page that holds the data.
If you prefer plain arrays, get_field() returns the whole repeater as a list of rows, each keyed by sub field name:
<?php
$rows = get_field( 'team_members', $post_id );
foreach ( (array) $rows as $row ) {
echo esc_html( $row['name'] ) . ' — ' . esc_html( $row['role'] ) . '<br>';
}
// The first row only: $rows[0]['name']
Load the page. You should see one list item per row, in the order shown on the edit screen.
- If nothing prints, run the three-line check. It tells you which layer is failing:
wp eval '$id = 214; $n = "team_members"; printf( "count meta: %s\nreference: %s\nget_field: %s\n", var_export( get_post_meta( $id, $n, true ), true ), var_export( get_post_meta( $id, "_$n", true ), true ), gettype( get_field( $n, $id ) ) );'
Example output on a healthy post:
count meta: '2'
reference: 'field_6721c4e9a1b3f'
get_field: array
Read it this way:
count meta: ''or'0', withget_field: NULLorboolean, means no rows are saved for this post. Check the location rule and the post ID, then add rows and update.reference: ''withget_field: stringmeans the value rows exist but the reference is missing. That is the red row in the diagram. Go to step 6.- All three healthy but the page is still empty means the template is the problem. The usual causes are a wrong post ID, a field label used instead of the field name, or a loop that never calls
the_row(). See Troubleshooting.
- Repair missing reference rows after an import. Save this as
acf-repeater-refs.phpoutside the web root, for example in your home folder:
<?php
// Usage: wp eval-file acf-repeater-refs.php <post_id> <repeater_field_key>
// Writes the missing underscore reference rows for one top-level repeater.
list( $post_id, $key ) = $args;
$post_id = (int) $post_id;
$field = acf_get_field( $key );
if ( ! $field || 'repeater' !== $field['type'] ) {
WP_CLI::error( "No repeater field with key $key." );
}
$name = $field['name'];
$count = (int) get_post_meta( $post_id, $name, true );
$fixed = 0;
if ( get_post_meta( $post_id, "_$name", true ) !== $field['key'] ) {
update_post_meta( $post_id, "_$name", $field['key'] );
$fixed++;
}
for ( $i = 0; $i < $count; $i++ ) {
foreach ( $field['sub_fields'] as $sub ) {
$meta_key = "{$name}_{$i}_{$sub['name']}";
if ( metadata_exists( 'post', $post_id, $meta_key ) && get_post_meta( $post_id, "_$meta_key", true ) !== $sub['key'] ) {
update_post_meta( $post_id, "_$meta_key", $sub['key'] );
$fixed++;
}
}
}
WP_CLI::success( "Post $post_id: $count rows checked, $fixed references written." );
Run it with the key from step 2:
wp eval-file ~/acf-repeater-refs.php 214 field_6721c4e9a1b3f
Example output:
Success: Post 214: 2 rows checked, 7 references written.
Run it a second time and it should report 0 references written, which proves the rows are now complete. If it says 0 rows checked, the count row team_members is missing too. Set it to the real number of rows with wp post meta update 214 team_members 2, then run the script again. The script handles one top-level repeater; a nested repeater needs the same pattern one level deeper.
- Query posts by a value inside any row. WordPress can’t know which row number holds the value, so the meta key needs a wildcard. ACF’s documentation does this with a
posts_wherefilter, a hook that lets your code edit the SQLWHEREclause before WordPress runs it. The filter turns=intoLIKE. It uses$as the placeholder, because since WordPress 4.8.3 a%in a query value is swapped for a placeholder before your filter sees it. Add the filter only around your own query, so it can’t change other queries on the page:
<?php
function sh_team_role_where( $where ) {
return str_replace( "meta_key = 'team_members_$", "meta_key LIKE 'team_members_%", $where );
}
add_filter( 'posts_where', 'sh_team_role_where' );
$query = new WP_Query( array(
'post_type' => 'page',
'posts_per_page' => 20,
'suppress_filters' => false, // posts_where must run. get_posts() skips it otherwise.
'meta_query' => array(
array(
'key' => 'team_members_$_role',
'value' => 'Accountant',
'compare' => '=',
),
),
) );
remove_filter( 'posts_where', 'sh_team_role_where' );
On WordPress 5.3 and later, the meta query can match the key with a regular expression, with no filter at all:
<?php
$query = new WP_Query( array(
'post_type' => 'page',
'posts_per_page' => 20,
'meta_query' => array(
array(
'key' => '^team_members_[0-9]+_role$',
'compare_key' => 'REGEXP',
'value' => 'Accountant',
),
),
) );
Check either version from the shell:
wp eval '$q = new WP_Query( array( "post_type" => "page", "fields" => "ids", "meta_query" => array( array( "key" => "^team_members_[0-9]+_role$", "compare_key" => "REGEXP", "value" => "Accountant" ) ) ) ); echo implode( ",", $q->posts ), PHP_EOL;'
Example output:
214,377
Each ID is a page with at least one matching row. An empty line means no row holds exactly that value; values are compared as stored, so check the spelling against step 3.
Validate it works
Fetch the page and count the list items your template printed. Use your own URL:
curl -s https://example.com/team/ | grep -o 'class="team-member"' | wc -l
Example output:
2
The number should match team_members in step 3. Then look for the largest repeaters on the site, since those are the posts where the editor or a query slows down first. Replace wp_ if your table prefix differs (wp db prefix prints it):
wp db query "SELECT post_id, meta_value AS rows_saved FROM wp_postmeta WHERE meta_key = 'team_members' ORDER BY CAST(meta_value AS UNSIGNED) DESC LIMIT 5;"
Example output:
+---------+------------+
| post_id | rows_saved |
+---------+------------+
| 214 | 2 |
| 377 | 2 |
| 519 | 1 |
+---------+------------+
Finally, open page 214 in the editor, add a third row, update, and reload the front end. A third list item confirms the whole path from editor to template.
Troubleshooting
The page never finishes loading, or shows a white screen. A while ( have_rows() ) loop is missing the_row(). have_rows() only checks for a next row and never moves forward, so the loop runs forever until PHP hits its time limit. ACF’s documentation warns about exactly this. Add the_row(); as the first line inside every while, including nested ones.
A nested repeater prints nothing. The inner have_rows( 'skills' ) must run inside the outer loop, after the outer the_row(). Called before it, or after the outer endwhile, ACF has no current parent row. It then looks for a top-level field named skills, finds none, and returns false. The inner get_sub_field() calls read from the inner row only, so read parent values before the inner loop starts.
The repeater works in the content but not in the header, footer or a widget. Without a post ID, ACF reads from the current global post. In those places that is either nothing or the last post of another loop. Pass the ID as the second argument, as in step 4.
The loop shows every row except the first one. An earlier loop on the same page used break to stop after one row. ACF’s docs allow break, but the loop stays open, so the next have_rows() for the same field continues from row 1. ACF’s source code has a reset_rows() function for this case, though the online documentation doesn’t list it. Call reset_rows(); right after the break, or read the first row from get_field() as shown in step 4.
$row['name'] is empty, but the row has a name. You called get_field() with false as the third argument, which turns off formatting. Unformatted rows are keyed by field key, such as field_6721c4f8a1b40, not by sub field name. Drop the third argument unless you need raw values.
have_rows( 'Team members' ) returns false. That is the field label. Use the field name, team_members, or the field key.
When this is the wrong approach
If you mainly need to filter or sort posts by a value that lives inside rows, a repeater is a poor fit at scale. Every search inside rows is a pattern match on meta_key. A LIKE pattern that starts with fixed text can use the index on that column for the prefix, while REGEXP cannot use it at all, and both scan more rows as the table grows. Store the searchable value somewhere WordPress can index well: a taxonomy term, a separate custom post type for each member, or a flat meta key you update in an acf/save_post hook.
If editors keep adding hundreds of rows to one post, consider whether each row is really its own piece of content. Turn on the Repeater’s Pagination setting (ACF 6.0 and later) to keep the edit screen responsive. It changes only the admin screen, not the template or REST output. The broader tradeoffs, including Meta Box and Pods, are in ACF at scale compared with Meta Box and Pods.
If the data comes from an outside system and only needs displaying, a custom table or a cached API response may be simpler than writing thousands of meta rows. The official Repeater documentation lists every helper function if you need more than the loop above.
Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.