Due to a massive import of posts from another cms into wordpress I have ended with thousands of posts without any category.

I need to add all those posts to the default category. I've seen this similar question asked before here: How to find all posts without category and assign the "Uncategorized"? but my little knowledge of the subject does not allow me to know how to apply the solution.

The solution was:

you can use wp_set_post_categories() function. Get all of your posts(which you need to assign category) in an array and run a loop theough the array with wp_set_post_categories( $post_ID, $post_categories, $append ) passing needed parameter. It will assign the category to your posts.

Can someone explain to me how can I create the whole function and where and when do I run it?

I want to avoid the server to run out of memory for having so many posts to change.



1 Answer 1


You can use wp_insert_post like this:

$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 );

Ref: https://codex.wordpress.org/Function_Reference/wp_insert_post

use wp_update_post to update posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.