0

I've created a custom post-type along with a custom taxonomy and everything is fine, apart from one particular thing.

First off, my use case: I need it to act like a category system, however not be hierarchical (one level only), so I left both to the default setting of false for the hierarchical setting.

This has been fine, apart from one problem where it allows me to add a "sub-level" within the metabox.

First, the code:

register_post_type( 'letter',
                    array(
                        'labels' => array(
                            'name' => __( 'Letters' ),
                            'singular_name' => __( 'Letter' ),
                            //..........
                        ),
                        'public' => true,
                        'taxonomies' => array('letter'),
                        //........
                    )
);

// Our args for the custom taxonomy below
$args = array(
    'labels' => array(
        'name' => __('Recipients'),
        'singular_name' => __('Recipient'),
        //.....
    ),
    'meta_box_cb' => 'post_categories_meta_box',
);

// Register a custom taxonomy for our letter categories
register_taxonomy( 'recipient', 'letter', $args );

// Connect the post type and taxonomy together to be safe
register_taxonomy_for_object_type( 'recipient', 'letter' );

Now, as you can see I needed to set the meta_box_cb to post_categories_meta_box otherwise it would end up having the functionality of the tag metabox, so, now it looks like this:

enter image description here

So this is good, I get the category functionality that I like, however I also get the "Add New Recipient" where when you open it up it allows you to add a new "recipient" and select a parent recipient; this I do not want.

Is my only option to create a new callback function based on the post_categories_meta_box function and remove the respective code to get rid of that parent functionality?

I don't particularly want to do that as then it would break future updates to this function.

Is there another option?

  • Well, another dirty option would be using CSS to hide that field or remove that element using jQuery. – Abhik Aug 12 '18 at 13:50
  • @Abhik Hmm true, I guess if it comes down to it that may prove to be a better, yet dirty, option. – Brett Aug 12 '18 at 13:57
0

Given that it's a dirty option you could try this way:

function dirty_admin_customization() {

    // #newtaxonomy_parent
    $style = '<style type="text/css">#newrecipient_parent { display: none !important; }</style>';

    echo $style . "\n";

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

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.