I have a custom taxonomy that I would like to update on posts using a PHP function. An example of what I am trying to achieve is the following:

    [0] => WP_Term Object
            [term_id] => 183
            [name] => Spotlight
            [slug] => spotlight
            [term_group] => 0
            [term_taxonomy_id] => 183
            [taxonomy] => deals_tiers
            [description] => 
            [parent] => 0
            [count] => 5
            [filter] => raw
            [term_order] => 0

I am trying to set this on posts that have no data in these fields. This can be achieved in the backend by checking the "Spotlight" checkbox but I would like to do this via a function. Any help would be greatly appreciated.

  • On what event do you want to assign the terms to post? Or do you want to assign the terms to all posts without a term? – Faham Shaikh Jul 11 at 21:22
  • I have an event that is fired once a transaction is made. The function is actually in a conditional if a cookie is set and that is firing, I just don't know what to call to enter this data. – Scott Cop Jul 12 at 15:29
  • You will have to call wp_query with taxonomy parameters setting field as term_id, terms as '' and operator as =. – Faham Shaikh Jul 12 at 17:27
  • Thank you for your responses. I'm not sure I understand however. Would I call this instead of doing something like wp_set_post_terms? I was trying that and update_post_meta to no avail. – Scott Cop Jul 12 at 17:52
  • You are supposed to call wp_query like this. That will allow you to set up query data for theloop and in theloop you will call wp_set_post_terms – Faham Shaikh Jul 12 at 18:27

Your Answer

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

Browse other questions tagged or ask your own question.