Support

Account

Home Forums General Issues save_post new category Reply To: save_post new category

  • Oh, I have found solution faster than I thought.
    I must use acf/save_post but with 20 priority which runs after saving $_POST data.

    I’m taking subcategory value from post, checking if that term exist and add it to post using wp_set_post_terms.

    Code:

    function my_acf_save_post_after($post_id) {
    
    	require_once('wp-load.php' );
    	require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
    
        // bail early if no ACF data
        if (empty($_POST['acf'])) {
            return;
        }
    
        //get post value
        $parent_cat2 = get_field('curr_categories', $post_id);
        $test_cat2 = get_field('cat_test', $post_id);
    
        $term_id = term_exists( $test_cat, 'category', $parent_cat2 );
    
        wp_set_post_terms( $post_id, $term_id, 'category', true );
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);