Chanon raise his hand

Work like you don't need the money
Love like you'll never be hurt
Sing like nobody's listening
Dance like nobody's watching
More about me »

Flutter get group duplicate issue

Flutter is a very handy WordPress plug-in that overwrite basic custom fields and turn it to something that provide higher user experience. However, there are many issues that have not been fixed. Also flutter development team seem to stop developing this powerful plug-in since July, 2009.

Get duplicate group is one of the key issues. Flutter use javascript serialize function to allows user creates unlimited group of the input fields. Unfortunately, the default function to presents the group of values is not that accurate.

Default methodology to get duplicate groups

Flutter presents document on how to get the duplicate group here.

getGroupDuplicates ($fieldName);

This function return the number of duplicate group by the given field name. Then, developer need to loop through this number and use get function to present their desire fields. Please find an example of the usage below.

1
$count = getGroupDuplicates("myField");
2
for($i = 0; $i < $count; $i++):
3
echo get("myField", $i);
4
endfor;

The problem

Code above seem to be fine if user add group and click update at every time. However, if user decided to add group and remove it, then add another one before click on update/publish button – it will generate the interval between group index. For example, instead of grouping like myField[0] – myField[1] – myField[2] the group index may become myField[0] myField[3] myField[5]. The first case will be fine to use default method but the later one will have the problem as value of second index will be disappeared and the loop will never meet the index 5.

The solution

To avoid this issue, developer need to do manual query for the number of duplicate group, plus the index of each group number. Please consider the code below.

Function

1
function getGroupOrderIndex($field_name, $pid = NULL){
2
global $post,$wpdb;
3
if (!$pid) $pid = $post->ID;
4
$fields = $wpdb->get_results("SELECT DISTINCT group_count FROM " . RC_CWP_TABLE_POST_META . " WHERE post_id = " . $pid . " AND field_name = '" . $field_name . "' ORDER BY order_id ASC");
5
foreach($fields as $field){
6
$indexes[] = $field->group_count;
7
}
8
return $indexes;
9
}

This function query the index of each group by the given field name (not just the amount). Copy and paste the code above to functions.php in the theme folder. Then replace the default usage with the following code in your theme file.

Usage

1
$groupIndexes = getGroupOrderIndex("myField");
2
foreach($groupIndexes as $index):
3
echo get("myField", $index);
4
endforeach;

That’s it. Please excuse my English, if you have any question about this, feel free to use the comment below.