Twitter
Tutorial: Flutter
12. July 2009 · Author: Dale Crum
85 Comments

Tutorial: Flutter Duplicate Fields

Creating duplicate fields through Freshout’s WordPress plugin, Flutter has been a little on the mysterious side up to this point. No documentation seems to exist for this feature with the exception of simply repeating a php get_post_meta tag for each Custom Field. The true beauty behind the duplicate fields lies within the ability to utilize a single php function to echo all fields as they are created on the fly.

An example:

I set up a Custom Write Panel Page titled “Upcoming Events”. Within this page is a group and the option for duplication is checked. Let’s title this group “Event” and create a Custom Write Fields like so:

CUSTOM FIELD 1:

Name: event-title
Label: Event Title

CUSTOM FIELD 2:

Name: event-date
Label: Event Date

For example purposes, let’s say I’m giving a speech in California on the 5th of July 2011. The first text field, “Event Title”, might be filled in as “IT Security World 2011″ and the second field might be a text field showing the date of “July 5th 2011″. Initially echoing theses fields is straightforward:

<?php echo get_post_meta($post->ID, 'event-title', $single=true) ?>
<?php echo get_post_meta($post->ID, 'event-date', $single=true) ?>

If echoing individual, duplicate fields within multiple groups:

<?php echo get('event-title', 1); ?> // Duplicate Group 1, Field 1
<?php echo get('event-title', 2); ?> // Duplicate Group 2, Field 1

The above code works perfectly until the time comes to add in a second or third event that we would like to automate. Keep in mind we are simplifying the situation and the above is assuming that you would not be archiving the events, but merely wanting to show the latest five or six at any given time. This is all well and good until you start clicking that duplicate button and you realize the second or third event doesn’t actually show because the new fields created must have their own individual names in order to exist, something such as “Event1″.

After looking through the Flutter documentation we find a few functions that seem as though they should deliver what we need: getFieldDuplicates ($fieldName, $groupIndex) and getFieldDuplicates ($fieldName, $groupIndex). Unfortunately these only return true values and are not what we are really looking for. What we want is the ability to click the duplicate button and have a single function return the values from the first event group and the next event group and so on.

Not much help has been handed down through the Flutter forums and it seems if anyone does know how to work this, they have been doing a great job of keeping it a secret up to this point. I found only one person willing to shed any kind of light on the matter and as grateful as I am, this was still not enough. Finally through shear frustration I took it upon myself to solve the problem through php. Albert Yarusso was kind enough to assist me in this project. Below is the php function we compiled to display duplicated groups.

<?php $my_query = new WP_Query('page_id=1');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID; ?>

<?php $total = getGroupDuplicates('event-title');?>
<?php for($i = 1; $i < $total+1; $i++);?>
<?php echo "<div class='event-wrap'>";
      echo "<h2>" .get('event-title',$i,1). "</h2>";
      echo get('event-date',$i,1);
      echo "</div>"; ?>

<?php endfor;?>
<?php endwhile; ?>

Line 1-3:

<?php $my_query = new WP_Query('page_id=1');

To begin, we need to create a WordPress query using our page_id number where the duplicate Custom Fields are located. This could just as easily be a post id number with duplicate fields. The first three lines should be understood as a basic concept from WordPress. If these are not understood, please refer to the WordPress codex on Loops. NOTE: If using this code on a page such as single.php, use the basic loop: <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>. The above example is necessary only when calling on a specific post or page.

Line 5:

<?php $total = getGroupDuplicates('event-title');?>

NOTE: This is not the name of the entire group, but the name of the first duplicate field. In this case, these are duplicate groups and not duplicate Custom Fields. This is because we have set the group to duplicate and not the individual fields.

Line 6:

<?php for($i = 1; $i < $total+1; $i++);?>

Start a count for each duplicate group in an effort to process them and add them incrementally.

Line 7:

<?php echo "<div class='event-wrap'>";<

Insert a simple php echo to add a bit of CSS styling for our events. This is only included to demonstrate the ability to add in CSS styling. Any CSS styles are housed within the stylesheet.

Line 8:

echo "<h2>" .get('event-title',$i,1). "</h2>";

Here we are inserting a CSS H2 style tag to the event title to give it a better look, then echo the event titles incrementally and end the H2 tag.

Line 9:

echo get('event-date',$i,1);

Add the event date, which again is done incrementally.

Line 10:

echo "</div>"; ?>

It is then important to end the div tag which opened on line 7.

Line 12:

<?php endfor;?>

Then end the “for” function.

Line 13:

<?php  endwhile; ?>

Lastly, don’t forget to close up the WordPress query to finalize the loop.

Now when adding additional duplicate groups on the post or page, the code recognizes that there is more than one group and subsequently echos them all. With a few tries you should have this up and running for any manner of duplicate groups or fields.

85 Comments

  1. ollie,

    Because Flutter seems to have stopped development I have moved on to Magic Fields. This might be something to consider in the future but for now give this a try:

    <?php $total = getFieldDuplicates('nameHere', 1);?>
    <?php for($i = 1; $i < $total+1; $i++):?>
    
    <div class="style"><?php echo get('nameHere',1,$i)?></style>
    
    <?php endfor;?>

  2. ollie says:

    Hi,

    This is a really helpful article, thanks, I have one question though

    For duplicate fields is it as easy as using getFieldDuplicates instead of getGroupDuplicates or some other function?

    Thanks,

    Ollie

  3. Thanks so much for this, I’ve been wracking my brain and now you’ve opened the door to a multitude of cool stuff I can do now. Cheers.

  4. Simon,

    At this time I don’t believe it is possible unless an update is made. I suggest switching to Magic Fields if this is a must for you. Magic Fields allows for the drag and drop re-ordering of duplicate fields. Keep in mind that the function on this page will not translate to Magic Fields, you will need to use their new functions.

  5. Simon says:

    Say I’ve got a group of group of fields that can be duplicated and group contains a date. Is it possible to sort the groups by that date field?

    Is this possible across the entire category? Ex:

    POST NUMBER 1
    Custom Group (a)
    Group (a) date
    Group (a) title
    Group (a) desc

    Custom Group (b)
    Group (b) date
    Group (b) title
    Group (b) desc

    POST # 2
    across the entire category? Ex:

    POST # 1
    Custom Group (a)
    Group (a) date
    Group (a) title
    Group (a) desc

    Custom Group (b)
    Group (b) date
    Group (b) title
    Group (b) desc

  6. jesse says:

    I just realized this, and since I recommended the plugin to begin with, I will note this for the sake of everyone/anyone who followed my advice:

    The drag and drop ordering in magic fields doesn’t work with the function in this post, you have to use magic fields’ duplicate group function (comments translated from the original wiki spanish (http://wiki.magicfields.org/doku.php?id=es:dealing_with_groups_duplicate_groups_and_duplicate_fields):

    
    <?php
    //duplicate groups
    
    // getGroupOrder returns an array with the order of the groups
    // This parameter for this function is the name of the first field in the group (or possibly any field in the group)
    $members = getGroupOrder('member_name');
    foreach($members as $member){
    //The second parameter after get and get_image is to indicate the group to show
    echo get('member_name',$miembro)."”;
    echo get(‘member_exampleField’,$member).”";
    echo get_image(‘member_photo’,$member).”";
    }
    ?>
    

  7. Jesse,

    No updates on Flutter duplicate re-ordering. Thank you for posting the Magic Fields code to keep everyone up to date.

  8. jesse says:

    I’m in agreement with Will– update to Magic Fields. It is a fork of flutter but seems to be much more actively developed and documented. Besides that, it works alot better, and the drag and drop feature makes ordering duplicate groups really easy.

    It was very easy to migrate from flutter to magic fields, and there is a well written, simple tutorial on their website @ magicfields.org

  9. jesse says:

    Any update on how to order duplicate groups?

  10. Drew,

    What is happening in IE? Can you send a link to your website as well.

    Thanks

  11. Drew says:

    Hi, I am using this function and I am obviously doing something wrong. It seems to work fine in everything but Internet Explorer. Here is my code:

    <?php $total = getGroupDuplicates("homepage_slideshow_image");?>
            <?php for($i = 1; $i < $total+1; $i++):?>
            <?php echo '<li><a href="'. get("homepage_slideshow_link",$i, 1, 1).'"';
                 echo get_image("homepage_slideshow_image",$i,1) . "</a></li>"; ?>    
               <?php endfor;?>

    Thanks in advance!

  12. kumar says:

    Thank you very much indeed! Just what I was looking for :)

  13. jesse says:

    The answer to my below question is:

    
    <?php echo date( 'n.j.y',strtotime( get('gigDate',$i,1) ) ); ?>
    

    Takes a Date string that is used to order the custom fields and reformats it without leading zeros.

  14. jesse says:

    New question!

    I am using flutter now for a custom-field-based calendar function in a wordpress blog located @ http://www.beattrainsoundsystem.com/

    Before using flutter, I was formatting a date custom field using the following code:

    
    echo date('n.j.y',strtotime($gigDate));
    

    Now that I’m using flutter, I can’t use $gigDate anymore as a parameter because there are duplicates, and each duplicate is called “gigDate”. How can I get the duplicates to format in the same way?

  15. Will says:

    No worries guys. I switched up to magic fields last night. Now I can drag and order! Also, I feel like magic fields does a much better job of dealing with group duplicates. Also, switching from flutter to magic fields was very easy.

    Thanks!

  16. Kevin,

    Would you mind sharing a code snippet of how you managed to get the order through a custom field?

  17. Will says:

    hey man, thanks for this code…it’s amazing! just one thing, and yes i’m really new to flutter, but how would i order the duplicate groups? i made a custom field for each of my entries and assigned a number. just not to sure how to make it work…thanks for any help!

  18. Is there anyone out this with ideas about controlling the order of duplicate fields and group. I’ve setup a gallery using duplicate groups, and would love to output each gallery in the order specificied within WordPress, similar to the way a “menu_order” parameter would work.

    For now, I’m using a field to control the order using numbers… but it would be great to use the setup described above. Any ideas out there?

  19. zac,

    Untested but give this a try:

    <?php $total = getFieldDuplicates('mainImage');?>
    <?php for($i = 1; $i < $total+1; $i++):?>
    <?php echo get('mainImage',$i,1); ?>

  20. Hi there and thanks for writing this! This is the first site I have come across that explains anything and there is very little (NO?!) documentation on the flutter site for this great feature of the plugin. I am trying to use getFieldDuplicates to echo however many images are uploaded. It works fine if there are 2 or more images but nothing is shown if only one image is included. Can someone please help. Here is what I am trying to use to call the images…

    <?php
    $total = getFieldDuplicates('mainImage',1);
    for($i = 1; $i < $total+1; $i++){
    echo get('mainImage',1,$i);
    }?>

    Thanks for any help!

  21. I just figured it out. It was kind of a silly/obvious question to begin with but I think it’s a relevant point for other WordPress Newbies…

    In your example you are pulling duplicate fields from a specific post ID. But to get duplicate fields from the current page, you don’t need the new WP_Query if you are already in the loop. Because my code is published on a post template, I was already in the following loop:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    and therefore just had to delete the following to get it to pull only the current page:

    <?php $my_query = new WP_Query('page_id');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>

    as well as the subsequent

    <?php  endwhile;?>

    Thanks for your help again.

  22. Jesse,

    I was just about to suggest this, though I wasn’t sure if you were on a single.php or not. I will add this to the post, it’s a question we’ve received several times.

  23. Jesse,

    I’ve checked the links and you must have made some updates since this comment. Are you still needing assistance?

  24. Major bug: Every post is pulling every custom field from all posts, rather than that posts’ specific ID. As a result, all photos from all posts are displaying in every single post.

    <?php $my_query = new WP_Query($post->ID);
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID; ?>
      
    <?php $total = getGroupDuplicates('slideshowA');?>
    <?php for($i = 1; $i < $total+1; $i++):?>
    <?php echo "<li>";
                echo "<a href='";
                if($slideshowA != '') {
                  echo get('slideshowA',$i,1). "'>";
              } else {
                echo get('slideshowLi',$i,1). "'>";
              }
                echo "<img src='" .get('slideshowLi',$i,1). "'/>";
                echo "</a>";
                echo "</li>"; ?>
    <?php endfor;?>
    <?php  endwhile;?>

    Help! Thanks so much.

  25. Jesse says:

    I’m one step closer! The fields display, but the mark-up is messed up.

    Instead of displaying a div with class “blockQuote” as specified in my php echo tag, it gives me this:

    <div class=" EIP_textbox  EIP_postid330  EIP_mid_632"></div>

    And the fields aren’t displayed in the paragraph tags that I wrapper around them in the echo tag. Thanks again for your help.

  26. Jesse,

    Turn off the “Edit in Place” feature. Settings > Flutter > Other Options > Edit-n-place – uncheck this box.

  27. lukusdukus says:

    Boom!

    Just like that it works! it sure make much more sense to call the group name rather than the first iteration of the field within that group! but fantastic it works, this will open up wordpress a massive amount for me thanks very much for sharing the knowledge!

    Cheers
    Luke

  28. Jesse says:

    Yea… They’re all filled in. I can retrieve the original field (not the duplicate) using the get_post_meta tag, but nothing shows up using that code. Any other ideas? Thanks for your quick response!

  29. Jesse says:

    I’ve read all the other comments and tried their suggestions, and I can’t figure out why the code isn’t outputting anything. I’m sure of the page ID and the names of the custom group and fields.

    Here is the code from my Testimonials.php

    <div id="twoThirds">
     <?php $my_query = new WP_Query('page_id=330');
                while ($my_query->have_posts()) : $my_query->the_post();
                do_not_duplicate = $post->ID; ?>
          
                <?php $total = getGroupDuplicates('quoteGroup');?>
                <?php for($i = 1; $i < $total+1; $i++):?>
                <?php echo "<div class='blockQuote'>";
                           echo "<p>" .get('block-quote',$i,1). "</p>";
                           echo "<p>" .get('by-line',$i,1). "</p>";
                           echo "</div>"; ?>
    
                 <?php endfor;?>
     <?php  endwhile;?>        
    </div> <!-- End twoThirds -->

    The page I’m applying this to is: http://cambridgeuplighting.com/testimonials

    Thanks alot! This is going to help me out ALOT once I get it working.

  30. lukusdukus,

    This is the same issue Jesse is having. The following should solve the problem and I will make a better note of this in the tutorial.

    <?php $total = getGroupDuplicates('team-member-name');?>
     <?php for($i = 1; $i < $total+1; $i++):?>
     <?php echo "<div id='test'>";
                echo "<h2>" .get('team-member-name',$i,1). "</h2>";
                echo get_image('team-member-image',$i,1);
                echo get('team-member-description',$i,1);
                echo "</div>"; ?>

  31. Jesse,

    I’m not sure how I keep missing this. I’m going with it doesn’t make that much sense. Change your code to this:

    <?php $total = getGroupDuplicates('block-quote');?>
     <?php for($i = 1; $i < $total+1; $i++):?>
     <?php echo "<div class='blockQuote'>";
                echo "<p>" .get('block-quote',$i,1). "</p>";
                echo "<p>" .get('by-line',$i,1). "</p>";
                echo "</div>"; ?>

    We’re changing the getGroupDuplicates to the name of the first field, not the name of the the entire group. The whole group name is a more logical choice and seems to be throwing others off as well.

  32. Jesse,

    Are the fields populated with information? This can throw you off, if not just add some quick information in there and review the output again.

  33. lukusdukus,

    I believe this is the same issue Dee was having. Because the code is being used on page.php it is not necessary to get a specific page id. Try this:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
         
        <?php $total = getGroupDuplicates('meet-the-team');?>
        <?php for($i = 1; $i < $total+1; $i++):?>
        <?php echo "<div id='test'>";
              echo "<h2>" .get('team-member-name',$i,1). "</h2>";
              echo get_image('team-member-image',$i,1);
              echo get('team-member-description',$i,1);
              echo "</div>"; ?>
         
        <?php endfor;?>
    <?php endwhile; endif; ?>
    

    Also, be sure that the fields are populated with information, since without any information nothing will output. Let me know how this goes.

  34. lukusdukus says:

    Doc4

    apologies code outputting 0 was </code

Trackbacks

  1. Using Flutter for a Restaurant Menu in WordPress – sara cannon
  2. Flutter: Major step in using Wordpress as a CMS | Bryan Williams
  3. Tutorial: Flutter Duplicate Fields | Doc4 Advertising Agency | WpMash - WordPress News
  4. Tutorial: Flutter Duplicate Fields
  5. Tutorial: Flutter Duplicate Fields | Doc4 Advertising Agency
  6. How to get duplicate fields in flutter – rhizom

Leave A Comment

Please wrap any code within the <code> tag to display properly

Multicons is a multi-favicon code generator which automatically inserts the necessary meta tags for both favicons (site-wide and/or admin) and Apple Touch / iPhone icons. Not sure...

IE6 Upgrade Option utilizes the 25K script created by Free the Foxes: http://www.freethefoxes.com/ as a WordPress plugin. Originally this plugin utilized a smaller 7K script but it's...

The WordPress Plugin: WordPress Database Backup by Austin Matzko is one of the more intuitive backup plugins currently available and with no stern warnings to scare off the faint of...

While doing our usual run of site updates and code adjustments, we ran into a small issue: how to display WordPress bookmarks with both text and images. Utilizing the 'Links' tab (...

BookMaster is our answer to what we feel is an odd issue with the WordPress wp_list_bookmarks tag. For those that have exercised the use of wp_list_bookmarks, you are well aware that...