Support » Plugins » Hacks » Custom Taxonomy public or private?

  • Resolved chrismichaels84


    I have created a number of custom post types (stories, authors, reviews, and news). Stories acts as a master or primary for the others. I have also created a custom taxonomy for “stories” called “stories_master” that is hierarchical like categories. I then, attached that taxonomy to my other post types. All is fantastic.

    What I am trying to do is make the “stories_master” taxonomy visible in the post area (for new or edit reviews, authors, etc), but NOT visible under the administration menus. I don’t want users to be able to add categories in the taxonomy. The plugin creates the categories. Does that make sense? So far, I can get one or the other to work.

    Here is the relevant code:

    /** Here is where I create the taxonomy and attach it to the master "stories" custom post type **/
    add_action( 'init', function () {
    // Create the slug
    $sanatized = "stories_master";
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        //with all my labels
    $args = array(
    	'labels' => $labels,
    	'public' => true,
    	'show_in_nav_menus' => false,
    	'show_ui' => true,
    	'show_tagcloud' => false,
    	'show_admin_column' => true,
    	'hierarchical' => true,
    	'rewrite' => true,
    	'query_var' => true
    register_taxonomy( $sanatized, array( 'stories' ), $args );
    register_taxonomy_for_object_type( $sanatized, 'stories' );
    /** When I create the other post types "authors, reviews, etc", I add this taxonomy like so: **/
    'taxonomies'	    => array( 'post_tag', 'stories_master' )

    Is what I am trying to do even possible? Is there a work around? I guess I could just manually remove the link from the menus.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Ashok


    NOT visible under the administration menus. I don’t want users to be able to add categories in the taxonomy.

    If the menu is not visible in administration menus, how will you add more items in that taxonomy? Of you will already have some pre-created taxonomies and then you want to hide? Or maybe, only admin will be able to add taxonomy items but not other users?

    Sorry for too many questions, maybe I am missing something.

    Also, yes, you can manually remove the menu items from menus.

    I create a custom taxonomy when I create a custom post type. The two are linked. However, my plugin needs to create the custom taxonomy items, not the user.

    I want the plugin to create the taxonomy (category) items in very specific instances and I don’t want the user to have any control over created or deleting these items. They still have control over all other taxonomies.

    I ended up removing the menu items, and hooking into the edit-tags page to die() with an error if they try to edit that taxonomy. Still, I feel like there has to be a much less clumsy solution.

    What I have works. If anyone wants, I’d be happy to post the entire script to github if it would help anyone. Currently it creates as many cpts as you like based on an array, builds whatever custom taxonomies for that cpt, and even links some cpts as “masters” to others.

    Moderator bcworkz


    FWIW, the correct approach is to specify custom capabilities in the ‘capabilities’ section of the taxonomy registration. You can control who manages, edits, deletes, and assigns terms this way. The related menus will not show without the assigned capability.

    I don’t know if it’s worth changing things if what you have is working, I’m really just stating for the record.

    Along the same vein, any time I see a lot of CPTs being created, I need to question if the taxonomy capabilities are not being used to their best advantage. Why couldn’t you have one CPT and in addition, a taxonomy called ‘cpt’? Each real CPT you now have becomes a term in the ‘cpt’ taxonomy and is assigned to the one real CPT as is appropriate. It’s not a good idea to create a LOT of CPTs, they are re-registered on every single page load. Taxonomy terms are in the DB and only accessed as needed. Querying multiple taxonomies at once can get complicated, but it is doable. Again, I’m not suggesting you change anything if it’s working for you, just stating for the record. Thus there is no need to defend your decisions unless you feel like doing so.


    Thanks for the info. I hadn’t considered capabilities. Is there a “no one can do this” capability? Or, since I’m the only super admin, I could set it to that.

    I always welcome questions about design strategy. I am a design nerd anyway. Here is what I am ultimately accomplishing.

    I have a publishing company with several stories (books) out there. So, my master CPT is “Stories” with its own tags and a couple taxonomies (genres, market, etc).

    On the site, I also want a few slave cpts like reviews, news, and authors. The idea is that there may be one Author (a post) that is attached to multiple stories. Same for news and reviews. I want to be able to query like so “get a list of authors that are attached to the story “Phantom Hearts” (post id 76).

    To do this, I have a system that creates a cpt “Stories” and calls it master. Then it creates whatever “slaves.” It also creates various taxonomies for each post type as needed. Then, it makes a “stories_master” hierarchical taxonomy that is shared between all the slaves (reviews, authors, news). Every time a new post is created under Stories, a new category item is created under “stories_master” thus allowing a new Author post to be assigned to the category “Phantom Hearts” which is identical to the post title “Phantom Hearts” under the stories CPT.

    Wow, that sounds complicated, but its only 400 lines of code, heavily commented. I only had a week to get this going (in my spare time) so I don’t know if I thought it through correctly. I’m not really a wordpress ninja (more of a symphony components/composer guy), so I’d love to know any feedback on how I may have done this more efficiently.

    In any case, I got it working 🙂 Thank you.

    Moderator bcworkz


    “No one can do this” is achieved by requiring a capability that no one has. Capabilities always imply someone can do something, though in custom code it is possible to reverse the logic. That probably adds unnecessary confusion though. Never check if something can be done by role, always check capability only. Roles alone are an overly constrained concept.

    I’m glad I didn’t offend you in questioning your approach, I never know what sets some people off 🙂

    Like I first mentioned, instead of a CPT structure, you could use another taxonomy structure where instead of a new CPT, you add a term to the taxonomy and attach it to the post. A related example is many themes support post “formats”, which could have been CPTs much like yours, but they are just another taxonomy. The format selected attaches a term to the post which determines which template is loaded to display the post.

    Even with a default WP installation, one could conceivably query for all posts with format “gallery” by author “art critic” in category “dada” with any child tag of “3D”. I don’t think your setup is that much more complex.

    Conceptually, there’s nothing wrong with using CPTs as an organizational device. It’s just that if there becomes a LOT of different CPTs, it drags down the site. Performance wise, taxonomies work better. Until you become very involved in WP, you couldn’t have known this, so your approach would otherwise be quite reasonable.

    Unless you truly are going to end up generating a huge number of different CPTs, I don’t think it’s worth changing anything, except possibly if your site will be very heavily trafficked. You’ve achieved the most important criteria… it works 🙂

    Thank you, I’ll keep an eye on it. Right now there are only 5 cpts. The reason I opted for cpts was so I could do individual templates for each cpt single and archive. But, I guess I could do the same thing with conditional tags.

    Great discussion. I always enjoy hearing how others do thing.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Taxonomy public or private?’ is closed to new replies.