How to Unassigns Categories from Posts in WordPress

Below are a couple snippets and descriptions as to how you can remove posts from a particular category:

I think that you should do something like this:

1. List all categories from a post and assign then to an Array
2. unset the ID from the category you wish to remove
3. wp_update_post( array(‘ID’ => $post->ID,’post_category’ => $arraywithcats));

Here’s a look at some code:

# full code to use in loop (category is in $cat variable) is:

$kategorie_wpisu = wp_get_post_categories( $post->;ID );
unset ( $kategorie_wpisu[$cat] );
wp_update_post( array( 'ID' => $post->ID, 'post_category' => $kategorie_wpisu ) );
unset ( $kategorie_wpisu );

I’ve pulled the above two discussions from a WP Hackers thread. I’ve actually needed to do this via plugin under bulk and I had used the same general method to remove WordPress posts from categories. It really works pretty much the same with settings posts to categories as well when you’re using the methods above.

Post written by Ed Reckers

Founder and lead web development consultant at Red Bridge Internet : San Francisco WordPress Developers and Consultants.

One Response to “How to Unassigns Categories from Posts in WordPress”

  1. peter

    How can I implement the code to add a category to a post without removing the other categories?

    $post = get_post(12);
    $post->post_category = array (106);

    so the post with ID = 12 i want to add it to the category 106
    but with this code the other categories are removed

Leave a Reply