Support

Account

Forum Replies Created

  • Thanks @johnblogger . I tried using wp_insert_category but failed , instead i solved the problem using wp_insert_terms. Here is the code:

    add_action('acf/pre_save_post', 'my_save_post');
    
    function my_save_post($post_id)
    {
    
    	if (isset($_POST['acf']['field_63be99db578f3'])) {
    
    		$cat_ID = get_cat_ID(sanitize_title_for_query($_POST['acf']['field_63be99db578f3']));
    
    		// Check if category exists
    		if ($cat_ID == 0) {
    
    			$cat_name = sanitize_text_field($_POST['acf']['field_63be99db578f3']);
    			$cat_slug = sanitize_title_with_dashes($cat_name);
    
    			wp_insert_term($cat_name, 'category', array('slug' => $cat_slug));
    		} else {
    			echo 'That category already exists';
    		}
    	}
    }
Viewing 1 post (of 1 total)