Simplifying Flutter duplicate groups and fields
Flutter or Fresh-pages is a really handy plugin for WordPress. With it, you can quite easily create custom write panels, fields and groups of information.
Having worked with this plugin quite extensively recently, I have encountered a few issues which have proved somewhat frustrating, dealing with duplicate groups or fields in particular. Flutter uses a counter based system in which to access duplicate groups. Simple enough when using a standard for loop and all the duplicates are in the correct order – but somewhat tricky to handle when say, deleting one of the duplicates.
I’ve since resorted to writing a function to make handling these duplicates and a number of associated problems whilst looping through them much, much simpler.
Which brings us to the following function – which you can paste straight into your theme’s functions.php file :
function jt_get_flutter_duplicates($field, $group){
if (is_string($group)){
$numfiles = getGroupDuplicates($group);
$isgroup = true;
} else {
$numfiles = getFieldDuplicates($field, $group);
$isgroup = false;
}
$return = false;
if ($numfiles > 0){
$return = array();
$count = 1;
$tmp = "";
$first = "";
$total = ($numfiles > 1) ? 100 : 1;
while($count <= $total){
if ($isgroup){
$value = get($field, $count, 1);
} else {
$value = get($field, $group, $count);
}
if (empty($value) || $value == $first || $tmp == $value){
$count++;
continue;
} elseif ($count == 1){
$first = $value;
}
$tmp = $value;
$return[] = ($isgroup) ? $count : $value;
}
}
return (is_array($return) && count($return) > 0) ? $return : false;
}
I’m not going to go into too much detail right now, but essentially what this function does is gets the required field or group duplicates, loops through them (up to a max of 100 iterations just to be safe) and places them into an array containing the correct index values (if a group) or an array of field values (if a field).
Group duplicates
Set the first parameter to the name of a field within the group which should contain a unique value (such as name, or caption). The second parameter can be any field within the required group.
$groupItems = jt_get_flutter_duplicates("news_image", "news_image");
if (is_array($groupItems) && count($groupItems) > 0){
foreach($groupItems as $i) {
echo get_image("news_image",$i, 1, 1);
echo "<br />" . get("news_image_caption",$i, 1, false);
}
}
Field Duplicates
Set the first parameter to the name of the field you want to retrieve. Set the second parameter to the group index you want to retrieve the field duplicates from. For instance, you could have 1 group which can be duplicated, containing 3 fields, one of which can be duplicated. When looping through the group duplicates, you’ll want another sub-loop to handle the duplicated field contents. The function will return an array of values which you can then use in a standard foreach loop.
$groupItems = jt_get_flutter_duplicates("office_title", "office_title");
if (is_array($groupItems) && count($groupItems) > 0){
foreach($groupItems as $i) {
echo get("office_title",$i,1,false);
$contacts = jt_get_flutter_duplicates("office_contact", $i);
if (is_array($contacts) && count($contacts) > 0) {
foreach($contacts as $contact){
echo "<br />Contact Person: " . $contact;
}
}
}
}
This is of course a quick fix and by no means infallible, so I’d love to hear if you have any improvements or a simpler way of dealing with this particular issue.

Really nice work. Sometimes I got one group twice but this could be “fixed” by just saving the post again.
Fantastic function, thanks a ton! I don’t know if I’d get anywhere with Flutter if it weren’t for people like you. Thanks!
I’m trying to use your function to call the values of a duplicated field.
It doesn’t seem to be working for me, maybe you can help me.
Here’s my code in the template:
$musictype = jt_get_flutter_duplicates("place music genre", "place general information"); if (is_array($musictype) && count($musictype) > 0) { foreach($musictype as $musictype){ echo "".$musictype.""; } }Nothing is unchanged in the code you instructed to paste into the functions file.
Apologies on the delayed response.
There seems to be a basic error in your for loop : foreach($musictype as $musictype) – this won’t work, essentially you’re overwriting the array with it’s first element.
Something like foreach($musictype as $item) would work.
Thank you. Thank you. Thank you. I jumped into a wordpress project with flutter without realizing the repercussions and was investigating deinstallation/total-wp-reinstall options before finding your blog. You’ve saved me hours of work.
Hi Jeff,
You’re so welcome. Glad that I’ve managed to help a fellow developer. Good luck with the rest of your project 🙂
This seems to work fine in everything but IE. What am I doing wrong?
0){foreach($groupItems as $i) {
echo "". "<a href='". get("homepage_slideshow_link",$i, 1, false)."'";
echo get_image("homepage_slideshow_image",$i, 1, 1). "";
}
}?>
Hi Drew,
I’m not too sure what it is you’re trying to do, as the code I posted originally has nothing to do with browsers. I have however spotted some errors in the code you’ve provided.
For one, you’re not closing the anchor tag off, and secondly, you’re not wrapping the image inside the anchor tag.
So try this :
echo “”. “<a href='”. get(“homepage_slideshow_link”,$i, 1, false).”‘ rel=”nofollow”>”;
echo get_image(“homepage_slideshow_image”,$i, 1, 1). “</a>”;
If you need any further help, feel free to send me an email and we can take it from there: calisza at gmail dot com
Thanks; very nice. I thought I had that plug-in figured out until I deleted duplicate items.
I stumbled across this just trying to find some flutter/magic fields documentation for the duplicate groups functions, and man, have you saved me some serious time. Thanks very much for sharing this bit of code.
You saved my day !! Thank you.