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?

2
  • Well, another dirty option would be using CSS to hide that field or remove that element using jQuery.
    – Abhik
    Commented Aug 12, 2018 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
    Commented Aug 12, 2018 at 13:57

2 Answers 2

2

This blog post by "Gazchap" deals with exactly the situation you are, and they updated it after publishing to your follow-up problem:

Fortunately, version 4.4 of WordPress introduced a filter – post_edit_category_parent_dropdown_args – that could be used to control the parent terms shown in these meta boxes. It’s designed to let the developer change the terms listed, for example excluding certain categories, or only showing “top level” parent terms and not their descendants. There is no control that is designed to stop the menu being shown at all, but there is one that allows us to trick WordPress into hiding the parent drop-down select.

Here’s the filter that you need:

add_filter( 'post_edit_category_parent_dropdown_args', 'hide_parent_dropdown_select' );

function hide_parent_dropdown_select( $args ) {
    if ( 'YOUR_TAXONOMY_SLUG' == $args['taxonomy'] ) {
        $args['echo'] = false;
    }
    return $args;
}

Why does this work? The echo argument is set to true by default, and makes WordPress echo the dropdown select into the meta box. By setting this to false, WordPress instead returns the HTML instead, so it doesn’t get rendered to the browser.

Note that of course YOUR_TAXONOMY_SLUG should be replaced with the slug of your custom taxonomy.

Anyone attempting this hack should definitely read the blog post in full, it also has some other useful tips.

2
  • Very interesting. Been awhile since I needed this, but if I do again I will be sure to give this a try :)
    – Brett
    Commented Sep 4, 2020 at 12:35
  • 1
    Important note: This solution only works, if the given post type does not use Gutenberg (either support for editor is disabled or forced to use Classic Editor). See bug here: github.com/WordPress/gutenberg/issues/13816 Commented Jan 6, 2021 at 13:34
1

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' );
1
  • Important note: This solution only works, if the given post type does not use Gutenberg (either support for editor is disabled or forced to use Classic Editor). Once Gutenberg is used, the meta box is rendered by React with a different markup, and this CSS would not work with that. Commented Jan 6, 2021 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.