How to Make a Category Always Selected?

This can be done with jQuery.

In this example, the modifier is printed when editing an existing post (admin_head-post.php) and when writing a new one (admin_head-post-new.php).

There’s a condition to check for the correct post type, as this hooks work with posts, pages and custom post types.

The “Most used” tab is being hidden. To address the categories in that tab, use the identifiers #in-popular-category-CAT_ID.

add_action( 'admin_head-post-new.php', 'wpse_72603_default_categories' );
add_action( 'admin_head-post.php', 'wpse_72603_default_categories' );

function wpse_72603_default_categories()
{
    global $current_screen;

    // If not our post type, do nothing
    if( 'post' != $current_screen->post_type )
        return;
    ?>
    <script language="javascript" type="text/javascript">
        jQuery(document).ready(function($) 
        {
            // Hide the "Most used" tab
            $('#category-tabs .hide-if-no-js').remove();

            // Tick the checkboxes of categories 3 and 9
            $('#in-category-3, #in-category-9').attr('checked', true);

            // Disable the clicks in categories 3 and 9
            $('#in-category-3, #in-category-9, #in-popular-category-9')
            .click(function() { return false; });
        });
    </script>
    <?php
}

Plugin of interest: Category Checklist Tree.

On the post editing screen, after saving a post, you will notice that
the checked categories are displayed on top, breaking the category
hierarchy. This plugin removes that “feature”.

Leave a Comment