Articles
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
Trackbacks
- Using Flutter for a Restaurant Menu in WordPress – sara cannon
- Flutter: Major step in using Wordpress as a CMS | Bryan Williams
- Tutorial: Flutter Duplicate Fields | Doc4 Advertising Agency | WpMash - WordPress News
- Tutorial: Flutter Duplicate Fields
- Tutorial: Flutter Duplicate Fields | Doc4 Advertising Agency
- How to get duplicate fields in flutter – rhizom
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:
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
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.
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.
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
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):
Jesse,
No updates on Flutter duplicate re-ordering. Thank you for posting the Magic Fields code to keep everyone up to date.
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
Any update on how to order duplicate groups?
Drew,
What is happening in IE? Can you send a link to your website as well.
Thanks
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:
Thanks in advance!
Thank you very much indeed! Just what I was looking for
The answer to my below question is:
Takes a Date string that is used to order the custom fields and reformats it without leading zeros.
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:
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?
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!
Kevin,
Would you mind sharing a code snippet of how you managed to get the order through a custom field?
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!
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?
zac,
Untested but give this a try:
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…
Thanks for any help!
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:
as well as the subsequent
<?php endwhile;?>Thanks for your help again.
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.
Jesse,
I’ve checked the links and you must have made some updates since this comment. Are you still needing assistance?
Sorry… here a few example URLs:
http://cambridgeuplighting.com/quincy-marriot-up-lighting
http://cambridgeuplighting.com/harvard-club-boston-uplighting
http://cambridgeuplighting.com/cambridge-multicultural-arts-center-up-lighting
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.
Help! Thanks so much.
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.
Jesse,
Turn off the “Edit in Place” feature. Settings > Flutter > Other Options > Edit-n-place – uncheck this box.
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
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!
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
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.
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.
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:
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.
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.
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:
Also, be sure that the fields are populated with information, since without any information nothing will output. Let me know how this goes.
Doc4
apologies code outputting 0 was </code