Reputation: 215
I want to update the terms for existing post. For example I have taxonomy "job_listing_type" and here are the terms of this taxonomy. Babysitting, Lawn Mowing, Leaf Raking, Pet Sitting, Plant Watering, Snow Shoveling. Its job post site. Now I have posted a job and selected "Babysitting" category for this post. Right now "Babysitting" term is set for a post. Now I want to edit my job post and want to set "Lawn Mowing" category for this post. I am using this code
wp_set_post_terms( $_POST['postid'], $_POST['job_type'], 'job_listing_type' );
//$_POST['postid'] has post id.
// $_POST['job_type'] has name of term. In this case, it is Lawn Mowing
//job_listing_type is custom taxonomy
Now I want to unset the previous set term (Babysitting) and then set new one which is "Lawn Mowing" but above code adds a new term "Lawn Mowing" and then sets it. As I understand from codex, it should first check, if the term doesn't exist then it should add new one and set it. I also used
wp_set_object_terms( $_POST['postid'], $_POST['job_type'], 'job_listing_type' );
but both have the same result thanks in advance
Upvotes: 1
Views: 9996
Reputation: 135
The issue is with functions 4th argument wp_set_post_terms
use it as
wp_set_object_terms( $_POST['postid'], $_POST['job_type'], 'job_listing_type', false );
if you will use this with true 4th argument it will add new taxonomy each time by deleting existing one
hope this will helpful.
Upvotes: 0
Reputation: 151
I had similar problem. I resolved it by making sure the ID is an array of Intervals and not a string or array of strings.
You can do it like so:
$integerIDs = array_map('intval', $_POST['job_type']);
$integerIDs = array_unique($integerIDs);
wp_set_post_terms( $_POST['postid'], $integerIDs, 'job_listing_type' );
its also recommend by the official docs: https://codex.wordpress.org/Function_Reference/wp_set_object_terms
Upvotes: 1
Reputation: 791
I had also the same problem, but the below solution works for me.
The issue is in the code is that, when you get the ID of the term you have to send it in the array, like
wp_set_post_terms( $postid, array( $jobtype_id), 'job_listing_type' );
Hope this will work for you.
Upvotes: 1
Reputation: 5267
Custom Taxonomies can be a bit confusing when your consider how you register their hierarchial
setting.
Categories & Tags are very similar, but one major difference (and I think this might be the confusion here) is that Categories are hierarchical whereas Tags are not.
When you registered job_listing_type
, (using register_taxonomy
) did you set hierarchical to true or false?
If hierarchical
is set to true (meaning that it'll be similar to a post category) then the function wp_set_post_terms()
needs to receive that term's ID (and not it's name). You can use term_exists()
to get the id by the name and then use that id to call wp_set_post_terms()
If hierarchical
is set to false (meaning that it'll be similar to a post tag) then the function wp_set_post_terms()
will accept a name.
If I understand your question correctly, I believe you'll want to first get a term's ID, then use that to call wp_set_post_terms()
.
Upvotes: 5