Support

Account

Home Forums General Issues change taxonomy terms on post update not on post creation

Solving

change taxonomy terms on post update not on post creation

    • jkjkjk

    • April 7, 2021 at 10:35 pm

    Hi, can you help me please?
    I’ve got a function which hooks into the save post action.
    Unfortunately it only works on creating a new post but not when updating an existing post.
    What do i have to do make that happen?

    I would like to have the taxonomy term “ha_p_kategorie” changed based on the true/false field “ha_p_gastspiel” and
    the taxonomy term “ha_p_jahrgang” changed when the date in date picker field “ha_p_premiere” is changed.

    This is my code:

    function change_produktion_content($post_id) {
        if ( get_post_type( $post_id ) == 'ha_produktion' ) {
    		
    		// update taxonomy term based on true/false field ha_p_gastspiel
    		$gastspiel = get_post_meta($post_id,'ha_p_gastspiel',true);
    		if ($gastspiel == '1') {
    			wp_set_object_terms( $post_id, 'Gastspiel', 'ha_p_kategorie' );
    		} else {
    			wp_remove_object_terms( $post_id, 'Gastspiel', 'ha_p_kategorie' );
    		};
    		
    	    // update another taxonomy term based on date picker field ha_p_premiere
    	    $premierendatum = get_post_meta($post_id,'ha_p_premiere',true);
    	    $jahrgang = date("Y", strtotime($premierendatum));
    	    update_field('ha_p_jahrgang', $jahrgang);
    	    wp_set_object_terms( $post_id, $jahrgang, 'ha_p_jahrgang' );
    		
    	    $my_post = array();
    	    $my_post['ID'] = $post_id;
    	
    	    remove_action('acf/save_post', 'change_produktion_content');
    	    wp_update_post( $my_post );
    	    add_action('acf/save_post', 'change_produktion_content');
        }
    }
    add_action('acf/save_post', 'change_produktion_content');

    What am i doing wrong? or missing?

  • If you want to change things when a post is updated based on a value changing then you need to have you filter run before ACF updates the DB with the new values by setting the priority to something less than 10

    
    add_action('acf/save_post', 'change_produktion_content', 1);
    

    Then you can compare the value in $_POST[‘acf’] with the value in the DB.

    • jkjkjk

    • April 8, 2021 at 10:59 pm

    Hi John, i am super sorry!

    I had some faulty code above all that ACF stuff which i didn’t copied here.
    Just a little “return;” that broke everything.
    I am sorry for taking your time but also thankful for your fast reply!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.