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.
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.
Because WP Alchemy stores data as a serialized array, you have to do some weird stuff to prepare to insert repeating fields.
This article explains in detail what you need to do: http://www.2scopedesign.co.uk/wpalchemy-and-front-end-posts/
The short answer is that you need to insert a serialized array, and then insert each meta value individually, like this:
//add a serialised array for wpalchemy to work
$data = array('reviews_what_we_did','reviews_star_rating','reviews_name','reviews_email','reviews_staff_member');
$str = $data;
update_post_meta( $pid, '_single_reviews_meta_fields', $str );
//update the individual fields
add_post_meta($pid, 'reviews_what_we_did', $service);
add_post_meta($pid, 'reviews_star_rating', $rating);
add_post_meta($pid, 'reviews_name', $name);
add_post_meta($pid, 'reviews_email', $email);
add_post_meta($pid, 'reviews_staff_member', $staff);
However, this snippet only works for normal fields, not for repeating fields. Repeating fields add another layer of complexity.
This article explains how to insert data into repeating fields: https://wpalchemists.com/2014/09/wpalchemy-update_post_meta/
In your case, you would need to do something like this:
$fields = array('address', 'country', 'zipcode', 'website', 'contacts');
$str = $fields;
update_post_meta( $postid, '_contact_meta_fields', $str );
$contactfields = array(
array(
'department' => $departmentvalue,
'firstname' => $firstnamevalue,
'lastname' => $lastnamevalue,
'title' => $titlevalue, // etc. - add all of your fields to the array
)
);
add_post_meta($postid, 'address', $addressvalue); // do this for each of your non-repeating fields
add_post_meta($postid, 'contacts', $contactfields); // this adds the repeating fields
It's really helpful to print_r the post meta so that you can see how WPAlchemy creates a serialized array as well as each meta value.