# Require a Real Category Before Publishing

> Stops posts from being published while they are only in "Uncategorized" or have no category, and shows the writer a clear message in the block or classic editor.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: editorial, categories, publishing
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/content-require-real-category/

## Code

```php
$wpide_has_real_category = function ( $category_ids ) {
    $category_ids = array_filter( array_map( 'absint', (array) $category_ids ) );
    return (bool) array_diff( $category_ids, array( (int) get_option( 'default_category' ) ) );
};

// Block editor and REST API.
add_filter( 'rest_pre_insert_post', function ( $prepared, $request ) use ( $wpide_has_real_category ) {
    $status = $request->get_param( 'status' ) ? $request->get_param( 'status' ) : get_post_status( $prepared->ID ?? 0 );
    if ( ! in_array( $status, array( 'publish', 'future' ), true ) ) {
        return $prepared;
    }

    $categories = null !== $request->get_param( 'categories' ) ? $request->get_param( 'categories' ) : wp_get_post_categories( (int) ( $prepared->ID ?? 0 ) );

    if ( ! $wpide_has_real_category( $categories ) ) {
        return new WP_Error( 'wpide_category_required', 'Please choose a category other than "Uncategorized" before publishing.', array( 'status' => 400 ) );
    }

    return $prepared;
}, 10, 2 );

// Classic editor.
add_filter( 'wp_insert_post_data', function ( $data, $postarr ) use ( $wpide_has_real_category ) {
    if ( 'post' !== $data['post_type'] || ! in_array( $data['post_status'], array( 'publish', 'future' ), true ) || ! isset( $_POST['post_category'] ) || wp_is_serving_rest_request() ) {
        return $data;
    }

    if ( ! $wpide_has_real_category( wp_unslash( $_POST['post_category'] ) ) ) {
        $data['post_status'] = 'draft';
        set_transient( 'wpide_category_notice_' . get_current_user_id(), 1, 60 );
    }

    return $data;
}, 10, 2 );

add_action( 'admin_notices', function () {
    if ( get_transient( 'wpide_category_notice_' . get_current_user_id() ) ) {
        delete_transient( 'wpide_category_notice_' . get_current_user_id() );
        echo '<div class="notice notice-error"><p>The post was saved as a draft: choose a category other than "Uncategorized" to publish it.</p></div>';
    }
} );
```

## Notes

Drafts can still be saved; only publishing and scheduling are blocked.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Require a Real Category Before Publishing" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
