How to add post into a category programmatically

I’m new to WordPress and php,so sorry for my basic question.
I want to add some posts in a category programmatically.
For example every day i want to select some posts and insert them into a category with php code and for next day replace some other posts.
Is it possible? How?
Thanks in advance.

Related posts

Leave a Reply

2 comments

  1. You can use wp_insert_post https://codex.wordpress.org/Function_Reference/wp_insert_post

    See the Codex, here you have a lot of examples:

    // Create post object
    $my_post = array(
      'post_title'    => 'My post',
      'post_content'  => 'This is my post.',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_category' => array(8,39)
    );
    
    // Insert the post into the database
    wp_insert_post( $my_post );
    

    To update the Post see wp_update_post https://codex.wordpress.org/Function_Reference/wp_update_post