Change post categories while saving post

I’m trying to change post categories inside a save_post action callback function, but I get endless recursion, because wp_update_post method fires save_post action itself.

Maybe somebody did this before& Or there is a way to change post categories without using wp_update_post method?

Related posts

Leave a Reply

2 comments

  1. you can pass the new categories to the function, in the same way you add the post.

    $newcats = array(
    get_cat_id($catname_one),
    get_cat_id($catname_two),
    get_cat_id($catname_three),
    get_cat_id($catname_four)
    ); 
    
    $wpx_post_arr = array(
    'ID'=>$post->ID,
    'post_category'=>$newcats
    );  
    
    wp_update_post($wpx_post_arr);