Home Forums Front-end Issues How to get the data outside the loop?


How to get the data outside the loop?

  • Hiya,

    Can somebody help me how to get a value outside the loop?

    the loop:
    – Repeater (page)
    — Flexible Content (row)
    —- Flexible Content (columns)
    —— Repeater (elements)
    ——— this value i need outside the repeaters and flexible content.

    I hope somebody can help me, thnx. 🙂

  • Without knowing the exact index of each nested repeater it will be impossible to get a value from a specific nested sub field with such a complex setup. But in order to loop through this structure outside of the loop is to provide the post ID for the first

    if (have_rows('page', $post_id)) {
      while (have_rows('page', $post_id)) {
        // nested loops here

    If you do know the exact index at each lever you can use get_post_meta() as suggested by @willh, this is how the meta key can be constructed. nested field indexes start at 0 and not 1.

    $meta_key = "page_{$index}_row_{$index}_columns_{$index}_elements_{$index}_field_name";
    $value = get_post_meta($post_id, $meta_key, true);
  • Hi John,

    I am not a advanced php guy but i need to duplicate the loop again (first option) to make it work you say? I get the value now, thats good :-). But i was thinking maybe it was easier with way less PHP code.

    Other question: i get the values now… But how can i check if their is a duplicate value in that loop and that all the duplicated will be removed/not showing.

    What i now get:
    Cat, Cat, Dog, Rat, Mouse, Rat, Cat, Dog, Mouse

    But what i want to see:
    Cat, Dog, Rat, Mouse

  • Even for someone that might be considered and advanced php guy, your fieldset with 4 nested repeaters is something that I’d consider complex.

    Not exactly sure what you’re trying to do or why. What is the reason that you need to get a specific value from a specific field in all of these nested repeaters outside of the loop where they are going to be displayed?

  • Hi John,

    It’s so that a user can select a font (Google Font) and i can load it in the Googlefonts url:

    <link rel="stylesheet" href=" the value">

    But a user can do it with all different elements so thats why i don’t need double values in that loop.

  • There are several ways that you can do this, the most straight forward is to loop through all of your values outside the loop and gather all of the values into an array, checking to make sure there are not duplicates

    $values = array();
    // your loop starts
    // somewhere inside your loops where you're getting the value
    $value = get_sub_field('field_name');
    if (!in_array($value, $values)) {
      $values[] = $value;
    // after the end of the loop loop through the
    // collectionof $values and echo what is needed

    The second method would be to create an acf/update_value filter In this filter you can possibly look at the value and store some other post meta value that has the list so you can just get this value instead of doing the loop. This would be pretty complex and would essentially just move the code from the front end into the admin and would probably require additional coding.

    Another method would be to use an output buffer

    // your current loop for outputting content here
    // with additions as shown in the first option for gathering values
    // after the loop, output the link and font values
    // the echo the output buffer wherever it is the content needs to be displayed
    echo ob_get_clean();
  • Hi John,

    Thank you for your anwser! I did your first option and i only get the word array back in the echo so i did something wrong.

    Is it possible to show you some code here and that not everybody can see it?

  • You can’t just echo the array that you create, you’ll need to loop through the values array

    foreach ($values as $value) {
      echo $value;

    There may be other ways, but I don’t recall what the correct format for a google fonts url is and I don’t know what your values actually are.

  • That did the job! The format is just good it’s just some font names and thats it!

    I made from the echo:

    <?php foreach ($values as $value) {
    		echo '<link rel="stylesheet" href="' . $value .':100,300,400,500,700,900">';

    And every font load in so that is working nice now!

    Big thank you John!

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘How to get the data outside the loop?’ is closed to new replies.