Home Forums General Issues Get all CF's contained in Slugs Array


Get all CF's contained in Slugs Array

  • Hello, it turns out that I have a very large number of custom Fields grouped by one or more fields of type “Group” because I need to make many specific modifications before showing these fields in the WP Front-End, I have seen myself in need to obtain the different values ​​of the CFS within the ACF Loop. The problem is that using Loops generates a lot of code and I wanted to reduce it just by making different calls (with different arguments) to a function that would generate each part of the content and return its due value.

    In conclusion I want to make a function to which just by transmitting at least arguments like:

    • A hierarchically ordered array (since they are groups contained within each other) of group slugs.
    • An array of CF’s slugs that are contained within the last group mentioned in the group array.

    The function executes the Loop inside it, concatenates the values ​​in the same variable and when finished returns a variable of type String that contains the text resulting from obtaining the values ​​of the CF’s mentioned in the Array (For obvious reasons these CF’s must belong to the last group mentioned in the Groups array).

    I’ve tried it in a number of ways, like setting foreach to iterate groups, and CFs, making the function call itself until the final index of the two arrays is reached (Group Slugs and CF’s Slugs); but there is always something that does not work 100%. The last thing I have tried is to iterate the arrays with “Do While” loops, but unfortunately this was worse because now it shows nothing. I suspect that it is a problem in my logic and I have noticed that on other occasions the problem has been that executing the return of the function breaks all the cycles and I don’t know how to solve it.

    Below I share my code I hope you can give me a good hand, since I definitely give up I have been stuck for a long time in the same problem.

    function lgroupf($agroups, $acfs, $label=false, $theader='', $object=false){
      do {
          $group = current($agroups);
          if ( have_rows($group) ):
            while ( have_rows($group) ):
              $notlast = next($agroups);
              if ($notlast):
                $out1 = $theader?"<b>$theader</b><br>":'';
                do {
                  $ncf = current($acfs);
                  if (get_sub_field($ncf)):
                    $vcf = $object?get_sub_field_object($ncf):get_sub_field($ncf);
                    $nlabel = $label?key($acfs):false;
                    $out1.= $nlabel?"<b>$nlabel</b>: $vcf<br>":"$vcf<br>";
                  return $out1;
                  $notcf = next($acfs);
                } while ($notcf);
        } while ($notlast);
    echo lgroupf(['Mediainfo', 'mis-general'], ['ID Codec' => 'misg-id_codec', 'Content Format' => 'misg-formato'], false, 'MEDIA', false);

    For this case I have a field of type “Group” with the Slug “Mediainfo”, inside this there is another group with the Slug “mis-general” and within this there are 2 CF contents identified with slugs “misg-id_codec” and “misg-format”.

    Thank you in advance for your time.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.