I'm using WPAlchemy to create meta data for my custom post types.
One of my custom post types, 'vendor', is declared as $vendor_mb with the prefix of _vendor in WPAlchemy and has below meta fields.
- Address
- Country
- Zipcode
- Website
- Contacts <-- this is a repeating field created by have_fields_and_multi.
The 'Contacts' is a repeating field, created as below;
<!-- Repeating Fields for Contacts -->
<h4>Contacts</h4>
<?php while($mb->have_fields_and_multi('contacts')): ?>
<?php $mb->the_group_open(); ?>
<label>Contact Information</label>
<?php $mb->the_field('department'); ?>
<span>Department</span>
<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_field('firstname'); ?>
<span>First name</span>
<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_field('lastname'); ?>
<span>Last name</span>
<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_field('title'); ?>
<span>Title</span>
<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_field('email'); ?>
<span>Email</span>
<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_field('mobile'); ?>
<span>Mobile</span>
<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_field('phone'); ?>
<span>Phone</span>
<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_field('fax'); ?>
<span>Fax</span>
<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_field('remark'); ?>
<span>Remark</span>
<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_group_close(); ?>
<?php endwhile; ?>
Using this code, I have no problem adding meta data to the meta fields, including repeating fields, in Wordpress Admin UI.
The problem is how to use the data in the front-end (I'm uploading a csv file containing multiple contacts information - John Doe 1, John Doe 2, and John Doe 3.)
I tried to use add_post_meta, and it creates three _vendor_contacts. I also tried to use update_post_meta, and the latter contact overwrites the former contact, leaving only the last one contact in the _vendor_contacts.
I further tried to get the existing value inside _vendor_contacts before doing add_post_meta or update_post_meta as below, but it doesn't work and doesn't echo anything.
global $vendor_mb;
$vendor_mb->the_meta();
while( $vendor_mb->have_fields_and_multi( 'contacts' ) ) :
$vendor_mb->the_group_open();
$vendor_mb->the_value( 'firstname' );
$vendor_mb->the_value( 'lastname' );
$vendor_mb->the_group_close();
endwhile;
So, any example I can refer to where WPAlchemy repeating fields are used in front-end?
Thank you.