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!
📣 “ACF Chat Fridays”
— Advanced Custom Fields (@wp_acf) January 31, 2023
The ACF team holds their first open office hours this Friday! Come and talk ACF, and ask questions about building sites with the plugin.
We’d love to see you there!
📆 Friday 3rd Feb - 3pm UTC
👉 Register here - https://t.co/3UtvQbE4CU pic.twitter.com/oTwW9K1XQ0
© 2023 Advanced Custom Fields.
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.