Skip Navigation

[Resolved] Combine Custom Fields into Single Variable?

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 6 replies, has 3 voices.

Last updated by nereaD 9 years, 3 months ago.

Assigned support staff: Caridad.

Author
Posts
#18078

Hi,
I've got a series of custom fields that comprise an address (i.e. street, city, state, zip). In the context of a Content Template, is it possible to combine these fields into a single variable?

I'm using the 5Sec Google Maps plugin (hidden link) that will accept a value from a single custom field, but since my address involves multiple custom fields, I was wondering what a workaround might be.

Thanks for any suggestions.

#18084

Dear Carrie,

You can add some code to functions.php in your theme like this:

add_filter('save_post', 'combine_my_fields');
function combine_my_fields($post_id, $post) {
$address = get_post_meta($post_id, 'wpcf-address', true);
$address = ', ' . get_post_meta($post_id, 'wpcf-address', true);
update_post_meta($post_id, 'full-address', $address);
}

Each time you save a post, its fields are combined to save in 'my-address' for the map.

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#18088

Hi Caridad,
Thanks for the quick reply and help! In the example you give would I need to add a custom field called 'full-address' in my address custom field group?

Just wasn't sure how to tie it together.

Thank you!
Carrie

#18146

Dear Carrie,

The code above will create the full-address custom field for you when you save a post. You will have to re-save old posts.

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#18165

Okay, I see. In your example, does wpcf-address refer to a specific custom field or to a group of fields? The function is working, but it's coming back empty. Thinking I need to replace wpcf-address, but just wasn't 100% what that was referencing.

Thanks!
Carrie

#18211

Caridad
This answer solved user's issue.

Dear Carrie,

Sorry, I didnt explain that part too much. You have to start building up the address in the $address variable. Lets say you have 4 custom fields defined in types and their slugs are street, city, zip, state. The real names have a wpcf- prefix, the full example would be like this:

add_filter('save_post', 'combine_my_fields');
function combine_my_fields($post_id, $post) {
$address = get_post_meta($post_id, 'wpcf-street', true) . '<br />';
$address .= get_post_meta($post_id, 'wpcf-city', true) . '<br />';
$address .= get_post_meta($post_id, 'wpcf-zip', true) . ' ';
$address .= get_post_meta($post_id, 'wpcf-state', true);
update_post_meta($post_id, 'full-address', $address);
}

Your field names and format of the full-address may need to be adjust to your needs.

Regards,
Caridad

#533263

Hi!

I know this is old but it is just what I need.

So, this is my code

add_filter('save_post', 'combine_my_fields');
function combine_my_fields($post_id, $post) {
$address = get_post_meta($post_id, 'wpcf-street', true);
$address = ', ' . get_post_meta($post_id, 'wpcf-city', true);
$address = ', ' . get_post_meta($post_id, 'wpcf-zip', true);
update_post_meta($post_id, 'wpcf-direccion', $address);
}

but all I´m getting in the "wpfc-direccion" field is ","
What am I doing wrong?

Thank you!