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);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.