Skip Navigation

[Resolved] Toolset copy field value to another field

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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Mateus Getulio 1 year, 9 months ago.

Assisted by: Mateus Getulio.

Author
Posts
#2652309

I have used the following code snippet from your site to copy one field value to another.

add_action( 'save_post', 'copy_location_to_multiline_field_admin', 100, 3 );
function copy_location_to_multiline_field_admin( $post_id, $post, $update ) {
if ( $post->post_status == 'publish' && $post->post_type == 'nacft-membership' ) {
$location = get_post_meta( $post_id, 'wpcf-postcode', true);
update_post_meta( $post_id, 'wpcf-postcodeshort', $location );
}
}

I have two fields set up one named: wpcf-postcode and the other wpcf-postcodeshort
and this works fine however I would like to bring across only the first 4 characters, so if a postcode is BA22 1PJ it would copy across BA22 only.

Is there any coding I can add to this snippet to make this work and only copy the first 4 characters.

My site is hidden link

#2652485

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there,

Thank you for contacting our support.

Can you try using the function substr to extract the first 4 chars of the string to see if that accomplishes what you need?

You can do it with a code similar to this:

add_action( 'save_post', 'copy_location_to_multiline_field_admin', 100, 3 );
function copy_location_to_multiline_field_admin( $post_id, $post, $update ) {
    if ( $post->post_status == 'publish' && $post->post_type == 'nacft-membership' ) {
        $location = get_post_meta( $post_id, 'wpcf-postcode', true);
        $short_location = substr( $location, 0, 4 ); // Extracts the first 4 characters
        update_post_meta( $post_id, 'wpcf-postcodeshort', $short_location );
    }
}

Please give it a try and tell us the results.

Thank you.

Mateus

#2654029

Perfect that works great. I have another issue I'm also trying to solve below and wonder if you are able to help.

I have a map linked to an toolset image field where it uses a coloured pin to display on the map depending on which category they belong to. Red pin for Category 1 and Blue for Category 2. I would like to automate this process if possible.

The link to the actual map page is hidden link

I have a category set up named Membership Types with taxonomies set up as Category 1 and Category 2.

And an toolset image field set up named map-pin which I then use an image named pin-cat1.png image for Category 1 and pin-cat2.png image for Category 2

What I would like to happen is that when they belong to the Category 1 the toolset map-pin field automatically chooses the image pin-cat1.png and then if they belong to Category 2 the toolset map-pin field automatically chooses the image pin-cat2.png

Is this possible to do with some coding.

#2654085

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there.

Thank you for confirming the issue was fixed.

I kindly ask you to open a new ticket for each new question or issue you find.

This approach aligns with our support policy, which covers one issue per ticket.

Doing so will help other users who may be searching for similar solutions on our forum.

We thank you for your understanding.

Mateus