Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 948
Ahmad Vatani Avatar asked Dec 01 '22 16:12

Ahmad Vatani


1 Answers

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

like image 80
Adrian Preuss Avatar answered Dec 15 '22 03:12

Adrian Preuss