0

I have a function to set a custom meta keys values to a taxanomy, term.

$category20 = $fields['field_52dd3aa1d6bc0']; // gps
wp_delete_object_term_relationships( $post_id, 'gps' );
wp_set_object_terms($post_id, $category20, 'gps', true );

From GPS meta key, value is set to Yes. Also on other fields meta key value is the same for example Yes and No.

When i use wp_set_object_terms function to store a term, same values is stored under the same term-id identificator.

How can i fix this? Term ids is setting from slug?

Update:

This code works:

wp_insert_term(
  ''.$category20.'', // the term 
  'gps', // the taxonomy
  array(
    'description'=> 'GPS use',
    'slug' => 'gps-yes'
  )
);

But no more for remove:

wp_delete_object_term_relationships( $post_id, 'gps' );

Figured out, working solution to use meta key for taxanomy terms:

wp_delete_object_term_relationships( $post_id, 'gps' );
if (!term_exists($category20, "gps")) {
    wp_insert_term(
      ''.$category20.'', // the term 
      'gps', // the taxonomy
      array(
        'description'=> 'GPS use',
        'slug' => 'gps-yes'
      )
    );
}
wp_set_object_terms($post_id, $category20, 'gps', true );
1

The last (fourth) argument that you pass to wp_set_object_terms() is true, which, "If true, tags will be appended to the object. If false, tags will replace existing tags". It sounds as though you are instructing wp_set_object_terms() to do, if I understand you, exactly what you don't want it to do. I think that you need to set the last argument to false or just leave it out, as false is the default.

  • Problem is here, car/gps/yes and old-car/gps/yes the theme yes terms is for id 6. A rest of code use term-id for all terms, not for term parent -> term. Is there a any way to make a term labels for yes but slug and id for each term is different? – Foxsk8 Mar 18 '14 at 20:26
  • Sorry, I don't know what you are asking – s_ha_dum Mar 18 '14 at 20:28
  • If i use a term for different taxanomies on the same slug, label name, then i got a same term id on each terms. IDs is not unique. The only fix to to that is make with wp_set_object_terms function, like wp_set_object_terms($post_id, name, slug, 'air_conditioner', false ); with name and slug is different like name yes and slug yes-gps, if i could do that, i got a unique term slug with unique term id. – Foxsk8 Mar 18 '14 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.