Home Forums Backend Issues (wp-admin) hook to update taxonomy, depending on a field value


hook to update taxonomy, depending on a field value

  • Hey guys,

    i have 2 custom fields for rental and buy prices on every post in my custom taxonomy “products”.

    when i add either a rental price, or a price to buy (or both) and update my post, i need wordpress to add a term (buy-rental) to the post, or remove it if no price has been set.

    no big deal, with a hook to ‘save_post’, right? check if any value has been set and update terms accordingly:

    function imm_data_conform($post_id) {
        if (get_field('buy')) {
            wp_set_post_terms($post_id, '15', 'products', TRUE);
        if (get_field('rental')) {
            wp_set_post_terms($post_id, '15', 'products', TRUE);
        if ((get_field('buy') == '') and ( get_field('rental') == '')) {
            imm_remove_post_term($post_id, '15', 'products');
    add_action('save_post', 'imm_data_conform');

    the strange thing is, that it does not work on the first update.
    i have to press “update” once, wait for the page to reload and press “update” again. only then will the taxonomy change.

    i use the same code over ajax on the frontend (without the hook) and it works on the first try.

    i don’t get it… please point me into the right direction 😉

Viewing 1 post (of 1 total)

The topic ‘hook to update taxonomy, depending on a field value’ is closed to new replies.