Can custom taxonomies be displayed inside of a custom meta box?

The question:

I’m currently displaying custom taxonomies in a drop down menu. Each taxonomy has its own little wrapper in the sidebar. Is it possible to move the taxonomy to inside of my custom write box for ease-of-use purposes?

enter image description here

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

The following is taken from a “move author to publish box” task, but it should give you a starting point. Next you should take a look at “/wp-admin/edit-form-advanced.php” where you’ll find something about get_object_taxonomies() to see how your stuff get’s named, so you can remove and add stuff.

function wpse_remove_author_box() 
{
    if ( ! is_admin() )
        return;

    remove_meta_box( 'authordiv', 'post', 'normal' ); 
}
add_action( 'admin_menu', 'wpse_remove_author_box' );

function wpse_author_to_publish_box() 
{
    if ( ! is_admin() )
        return;

    global $post_ID;
    $post = get_post( $post_ID );
        ?>
        <div id="author" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">
            Author: 
            <?php post_author_meta_box( $post ); ?>
        </div>
        <?php 
}
add_action( 'post_submitbox_misc_actions', 'wpse_author_to_publish_box' );

Method 2

I was able to achieve this with custom taxonomies on a metabox I created for a custom post type of mine by doing the following:

First I added this to my functions.php:

add_action( 'admin_menu', 'remove_default_metaboxes' );
function remove_default_metaboxes() {
    // Remove 'Keywords' (like tags) metabox
    remove_meta_box( 'tagsdiv-keyword', 'my-custom-post-type-slug', 'side' );
    // Remove 'Groups' (like categories) metabox
    remove_meta_box( 'groupdiv', 'my-custom-post-type-slug', 'side' );
}

The first parameter of the function remove_meta_box() is actually an id generated by WordPress. If you are using non-hierarchical taxonomies (i.e. “Tags”), it will always be “tagsdiv-” followed by your custom taxonomy slug. If it’s a hierarchical taxonomy (i.e. “Category”) the id will be the slug followed by “div”, no spaces.

I then put the following code on my metabox template:

<div class="groups">
<?php
    $box = array(
        'args' => array(
            'taxonomy' => 'group'
        ),
        'title' => 'Groups'
    );
    post_categories_meta_box($post, $box)
    ?>
</div>

<div class="keywords inside">
    <?php
        global $post_ID;
        $post = get_post( $post_ID );
        $box = array(
            'args' => array(
                'taxonomy' => 'keyword'
            )
        );
        post_tags_meta_box($post, $box);
    ?>
</div>

Finally, note that in the case of “Keywords” or any other Tag-like taxonomy, this will not be enough. The tags metabox functionality is achieved via JS, which depends on selectors that are not created by the function above. In my case this was solved by adding the class “inside” to the keywords div, and by adding these lines to a separate JS file:

$('.keywords').each(function(){
    tagBox.init();
    return false;
}); 

Just make sure to call the JS after WP has loaded the default Post scripts. If for some reason you don’t want to add more scripts, you can achieve the same by mimicking WP’s structure like so:

<div id="side-sortables">
   <div id="tagsdiv-keyword" class="postbox inside">
       // post_tags_meta_box() etc
   </div>
</div>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment