4

Can anyone give me a function or an idea of the method I would need to use to hide categories from the selection box in wp-admin?

I have a custom post type and I would like my authors to be able to choose ONLY between 5 of those categories while they are editing their posts. I would like this to only be the case with the custom post type and NOT for regular posts.

1 Answer 1

6

Something like this should do it. Replace wpse_77670_getPermittedCategories() with however you select the array of permitted categories, and 'your_custom_category' with whatever your custom taxonomy is for your custom post type.

/**
* filter terms checklist args to restrict which categories a user can specify
* @param array $args arguments for function get_terms()
* @param array $taxonomies taxonomies to search
* @return array
*/
function wpse_77670_filterGetTermArgs($args, $taxonomies) {
    // check whether we're currently filtering selected taxonomy
    if (implode('', $taxonomies) == 'your_custom_category') {
        $cats = wpse_77670_getPermittedCategories();    // as an array

        if (empty($cats))
            $args['include'] = array(99999999);     // no available categories
        else
            $args['include'] = $cats;
    }

    return $args;
}

if (is_admin()) {
    add_filter('get_terms_args', 'wpse_77670_filterGetTermArgs', 10, 2);
}

Edit, to work with regular 'category' taxonomy on custom post type:

function wpse_77670_filterGetTermArgs($args, $taxonomies) {
    global $typenow;

    if ($typenow == 'tsv_userpost') {
        // check whether we're currently filtering selected taxonomy
        if (implode('', $taxonomies) == 'category') {
            $cats = array(89,90,91,92,93,94); // as an array

            if (empty($cats))
                $args['include'] = array(99999999); // no available categories
            else
                $args['include'] = $cats;
        }
    }

    return $args;
}

if (is_admin()) {
    add_filter('get_terms_args', 'wpse_77670_filterGetTermArgs', 10, 2);
}
6
  • Thanks for the help! I'm a bit confused though on what to replace those two code snippets with. I tried using wp_list_categories to get the 5 ids of the cats and using the name of the post type (tsv_userpost) but it didn't work and just gave 404 errors on wp-admin. Can you give me a little more explanation of what to put in those spots? Also, I'm not using a custom taxonomy for the categories. It is the same categories that I'm using for regular posts, I just only want to show 5 of them instead of all of them for the custom post type.
    – Eckstein
    Commented Dec 30, 2012 at 9:07
  • For the cat IDs, you can look them up in the admin; just hover over the category edit links and see what the term_id is. Then you can add them to an array: e.g. $cats = array(1, 2, 3, 4, 5);
    – webaware
    Commented Dec 30, 2012 at 9:25
  • I hate single-para comments! Anyway, for the category taxonomy: well, I guess you could add global $typenow; and test for $typenow = 'tsv_userpost' and use 'category' where I have 'your_custom_category'. Worth giving it a burl, I reckon!
    – webaware
    Commented Dec 30, 2012 at 9:27
  • OK, this is working: function wpse_77670_filterGetTermArgs($args, $taxonomies) { global $typenow; if ($typenow == 'tsv_userpost') { // check whether we're currently filtering selected taxonomy if (implode('', $taxonomies) == 'category') { $cats = array(89,90,91,92,93,94); // as an array if (empty($cats)) $args['include'] = array(99999999); // no available categories else $args['include'] = $cats; } return $args; } } if (is_admin()) { add_filter('get_terms_args', 'wpse_77670_filterGetTermArgs', 10, 2); }
    – Eckstein
    Commented Dec 30, 2012 at 20:10
  • Grr, sorry for the crappy formatting. Anyway, this is working for limiting the categories on the user-post page BUT it's removing all categories from the normal posts edit page. Hmmmm....
    – Eckstein
    Commented Dec 30, 2012 at 20:10

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.