How to add a custom metabox in the post add/edit screen for category choice?

Is there a way in WordPress that I can make a custom meta box in the post Add/Edit screen that includes a drop down menu to enable the post author to select the category of the post, and then save the choice of the author normally into the WordPress database?

Related posts

Leave a Reply

1 comment

  1. WordPress allows for multiple categories in a post, which is why this page includes a Categories meta box with checkboxes, rather than a drop down.

    If you do not see this meta-box when adding/editing a post, you’ll need to click on “Screen Options” in the upper right, then check “Categories”.

    If you do not see that option in Screen Options, then likely this has been disabled via a custom plugin or theme.

    But let’s assume you want to only allow one category per post. You’ll need to remove the current category meta box, and replace it with your custom meta box.

    So, in a plugin, or your theme’s functions.php file, you’ll want to do something like this:

    <?php
    function replace_categories_meta_box() {
      remove_meta_box( 'post_categories_meta_box' , 'post' , 'side' ); 
      add_meta_box( 'my_categories_meta_box' , 'post' , 'side' );
      add_meta_box('categorydiv', $label, 'my_categories_meta_box', null, 'side', 'core', array( 'taxonomy' => 'category' ));
    }
    add_action( 'admin_menu' , 'replace_categories_meta_box', 11);
    
    function my_categories_meta_box( $post, $box ) {
      # work off post_categories_meta_box, in wp-admin/includes/meta-boxes.php
    }
    ?>