Get the First Row of an ACF Repeater by Product or Post ID

Quick Summary

  • Load another post's repeater by passing its ID to get_field(), then take the first row at index 0 and read a sub-field by name.
  • Store the other post's ID in a Number field, or a Post Object field returning Post ID, and read it with get_field().
  • The Repeater field requires ACF PRO, and you should check that the repeater has a value before reading its first row.

Sometimes you need one value from an ACF repeater that belongs to another post, for example the first affiliate link of a product you picked on the current page. This snippet reads that product’s ID from a field, loads the product’s repeater and takes the first row. It needs Advanced Custom Fields PRO, because the Repeater field is a PRO feature.

PHP
$p1_id = get_field('product_one_id');//get id
$aff_link1 = get_field('affiliate_links', $p1_id );//REPEATER FIELD NAME

$aff_l1 = $aff_link1[0];
$aff_l1_link = $aff_l1['affiliate_link'];//sub-field name

<pre><?php echo $aff_l1_link; ?></pre>

How it works

  • get_field( 'product_one_id' ) returns the ID of the other post or product. Use a Number field, or a Post Object field with its return format set to Post ID.
  • get_field( 'affiliate_links', $p1_id ) loads the repeater named affiliate_links from that post. ACF returns it as an array of rows, and each row is an array of sub-field values.
  • $aff_link1[0] is the first row, and ['affiliate_link'] picks the sub-field with that name.
  • Replace the three field names with your own.

Using it in a template

The first lines are plain PHP, so wrap them in <?php ?> tags when you add them to a template file. The <pre> tag at the end is handy for checking the value; to show a real link, echo the value inside an href and escape it with esc_url() (why esc_url, esc_attr and esc_html matter).

If the repeater on the other post has no rows, there is no row [0] to read, so check that $aff_link1 has a value first.

If your theme relies on ACF repeaters like this, see my WordPress development services.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More