0

Is there a way to filter Wordpress save_post in a way that if more than one category is present, the Uncategorized category is unset/removed? This should works also on first post save

4

Yes. You can use the save_post action and do it here is some function that remove the default wordpress category if there is some other category selected.

I added some comments so you will understand the process.

function remove_uncategorized($post_id) {
    // get default category
    $default_category = (int)get_option('default_category');
    // check if the post is in the default category
    if(in_category($default_category, $post_id)) {
        // get list of all the post categories
        $post_categories = get_the_category($post_id);

         // count the total of the categories
        $total_categories = count($post_categories);

        // check if the post is in more than 1 category (the default one and more..)
        if($total_categories > 1) {
            // remove the default category from the post
            wp_remove_object_terms($post_id, $default_category, 'category');
        }
    }
}
add_action( 'save_post', 'remove_uncategorized' );
  • Thanks! Will this work also on freshly created (and yet not saved) posts? I mean on first saving.... – Riccardo Jan 31 '17 at 22:17
  • Yes its will work on new posts too. btw new posts doesn't choose you the default category if you select other category in the first save. – Shibi Feb 1 '17 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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