Skip to content

Instantly share code, notes, and snippets.

@palicko
Created May 29, 2017 15:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save palicko/a64d806d4b24462dc004487f8f3342ec to your computer and use it in GitHub Desktop.
Save palicko/a64d806d4b24462dc004487f8f3342ec to your computer and use it in GitHub Desktop.
ACF repeater via get_post_meta()
<?php
// Returns array with repeater subfields and its values
function repeater_get_post_meta( $repeater_key, $post_id, $sub_field_keys ) {
if ( ! $post_id ) {
$post_id = get_the_ID();
}
// get repeater field
$repeater = get_post_meta( get_the_ID(), $repeater_key, true );
$repeater_output = array();
if( $repeater ) {
// loop through repeater fields
for( $i = 0; $i < $repeater; $i++ ) {
// loop through subfields
for( $j = 0; $j < $sub_field_keys; $j++ ) {
$repeater_output[$i][$sub_field_keys[$j]] = get_post_meta( get_the_ID(), $repeat_key . '_' . $i . '_' . $sub_field_keys[$j], true );
}
}
}
return $repeater_output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment