Skip Navigation

[Resolved] Adding to a terms field

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 1.16 hours from now. Thank you for your understanding.

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 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 14 replies, has 2 voices.

Last updated by Farrel 7 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#558712

Hi

I need some help with some code. I am trying to add to a terms field that has multiple values. This is the code I am using.

$offices = get_term_meta( $vendor_id, 'wpcf-vendor-offices', false);
$office = implode(",", $offices);
$addoffices = ($office.','.$post_id);

update_term_meta( $vendor_id, 'wpcf-vendor-offices', $addoffices );

My problem is if there are 4 existing values in the array then the string is repeated 4 times.

I know I am doing something wrong, I just can't figure it out and need help.

Thanks

#558768

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Farrel,

Thank you for contacting our support forum.

I'm not sure what you are trying to achieve , what do you mean by adding a terms field that has multiple values ?

Is there an example that I can have a look at ?

Thanks,
Shane

#558774

Hi Shane

I have created a meta field for one of my taxonomies that allows multiple values. What I want to do is use a form that adds to those values already stored in that field.

In this case the field "wpcf-vendor-offices" contains several numbers for example: 205, 256, 289

Now I want to add to those existing numbers, so it then stores the following

205, 256, 289, 88

If I do it from the taxonomy screen in the admin I just press a button to add another value, but in my case I want to do it programmatically via a form. That's what I need the code to do, add another number to what is already being stored in that field, "wpcf-vendor-offices".

Thanks

#558785

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Farrel,

I see what you're saying now.

So you essentially have a comma separated string that you want to add more values to.

On your form are you just adding the next number to append to the end ? Because what you can do is to retrieve the currently entered value and the values that are stored in the field, convert it to an array, append the new value to the end of the array and then convert it back to a comma separated string and store it again.

Please let me know if this helps.
Thanks,
Shane

#558787

Hi Shane

I know the theory of what to do, I'm just having difficulty working with the array.

What you said is correct. I want to retrieve the existing array, append the new value to it, and then save the result.

I just need the code to do all that because it's not working right for me. Forget about my commas, there are no commas being saved, I'm using a standard field that allows multiple values, so if you can provide the code to do all that it will be appreciated.

Thanks

#558955

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Farrel,

Could I gain access to the page where you are performing the tests for this ?

I would like to see what exactly is being done. Also are you using a CRED form to do this ?

Please let me know.
Thanks,
Shane

#558972

Hi Shane

There really is nothing you need from me to achieve this. All you need to do is the following on any WordPress install:

1. Create a standard single line taxonomy term field and set it for multiple values.

2. Add 2-3 values from the admin so the field now has several values being stored.

3. Create code so you can add another value to the existing values programmatically without using the interface in the admin.

That's all I need, the code to be able to add another value.

To make it clear. I don't need the code to initiate the action, I just need the code to return the array and add another value. That's it, it's very simple. If you want to make it fire off a Cred action that's fine, but it's not going to matter in my case because I am only interested in the part where the values are retrieved and added to.

#558978

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Farrel,

There is a step missing here.

Now we can write the code to do this, where is the next value in the list going to come from ?

What action initiates the code to run. If this were to be implemented as is the code will not run at all, maybe there is a step I am missing.

I see where you are using implode which is not so correct. First you need to convert the string to an array using the explode function.
hidden link

Then you need to append your next value to the back of the array.
hidden link

Finally you need to implode that array so that its back to a comma separated string.

Please let me know if this was clear.

Thanks,
Shane

#558982

Hi Shane

I edited my previous post to be clear. I don't need the code to fire the action, just to do the update.

You can use any variable you like, don't worry about where it comes from. It can be $newitem.

I already have the rest of the code. As stated before my difficulty is getting the field values retrieved in the correct format, adding to it, and updating the field so it's saved in the correct format.

Thanks

#558989

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Farrel,

I understand but its difficult to assist when the code you provided has no context.

$offices = get_term_meta( $vendor_id, 'wpcf-vendor-offices', false);
$office = implode(",", $offices);
$addoffices = ($office.','.$post_id);

update_term_meta( $vendor_id, 'wpcf-vendor-offices', $addoffices );

As it stands now this code will live in the functions.php with no call to it. I need a context in order to assist better.

For e.g a good context would be, when a user updates a post run this code.

However if you stated that this code works fine then the advise provided here
https://toolset.com/forums/topic/adding-to-a-terms-field/#post-558978

Should allow you to achieve your goal with a few modifications to your code.

Thanks,
Shane

#559034

Hi Shane

I feel like we have a communication problem. I'm not having a problem with the execution of the code. I'm having a problem with how the data is being saved.

You keep referencing context and how the code functions when it's irrelevant. We are simply talking about a small piece of the code within the rest of my code, ie the data that is being retrieved and saved once the rest of the action takes place.

My code I showed you is not correct, it results in the values being saved 4 times.

Instead of getting this saved in the field as 4 separate values.

205
256
289
88

I am getting this 4 times.

205, 256, 289, 88
205, 256, 289, 88
205, 256, 289, 88
205, 256, 289, 88

It's a problem with how I am retrieving the array and then resaving it. The context makes no difference. We are talking about two processes only.

1. How the data is retrieved
2. How the modified data is saved.

I didn't include the rest of the code because that is not the problem. It would make no difference what happens before or after because the problem resides in the retrieval and saving of the data. In terms of the variable I am simply adding one number to the existing numbers

If this is still not clear can you please pass this support thread onto another engineer because we are not communicating very well.

No offense I just want this answered and I feel there is nothing more I can add to explain. I feel you were on the right track before when you were talking about the explode. That's all I am after, the actual code required. If I can't get this answered then I'm better off just trying to figure it out by myself with the trial and error method.

Thanks

#559041

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Farrel,

But from this I see no issue in how the information is retrieved or saved as these are the standard ways to retrieve the information using the wordpress function.

So looking at your 2 points there is nothing wrong with it.

However what could be wrong is the amount of times the code is being run. It could be executing multiple times. Because there is nothing wrong with the code you've sent me.

Thanks,
Shane

#559048

Hi Shane,

There is definitely something wrong with the code from an array point of view. It's doing it 4 times because there are 4 values in the array. It's also putting all the values in one string.

But I think I'm better off trying to figure this out by myself. Thanks anyway.

#559073

OK, I worked it out. Here is the solution for anyone needing it. It's very simple.

Don't use "update_term_meta" instead use "add_term_meta".

Example: add_term_meta( $vendor_id, 'wpcf-vendor-offices', $post_id);

That's it, just one line of code is needed and WordPress does the rest. It will add the new value to the existing values.

#559075

problem solved.