Restricting Users to view only Custom Taxonomies they have entered?

This can be done, but before I explain my approach, be forewarned that this is not for the faint of heart. I won’t give you the code for the solution here, but I’ll lay out how this can theoretically be accomplished.

Associating Taxonomy Term with User

The first issue that you need to solve with this is finding a way to associate taxonomy terms with users. By default, WordPress does not do this. WordPress records author information that is associated with posts of all types, but similar data is not recorded for taxonomy terms. So, to begin, you need to develop a method for adding meta data to your terms. I recently wrote an article about adding meta data to taxonomy terms. What you will need to do is add the user id as meta data to the terms whenever the term is saved. This would mean capturing the meta data and saving it in the taxonomy term create and edit screens, as well as in the main post write screen where users can create new terms. The two most important hooks that you will need for this are:

  • created_{$taxonomy}
  • edited_{$taxonomy}

These hooks are called as part of the wp_update_term function and would be an excellent place to add your term meta to associate the current author’s id with the term. With this accomplished, you would have your terms associated with your authors.

Filtering Terms By Author

Now that the authors are associated with the terms, you need to filter these terms when displayed. In other words, you need to identify when these terms are displayed in the admin, and filter them so that only the author’s terms are shown. I think the two most important areas to do this filtering would be in the taxonomy screen and the post screens (there may be other places to filter these terms, but these are the only two that come to mind at the moment). Your approach to this can vary a bit, but primarily, you will be looking for ways in which you can use add_filter to affect the terms that are displayed in these scenarios. For the write screen term display, you can use:

  • get_terms
  • wp_dropdown_cats

to help with tasks. These are relatively easy to utilize. Basically, the strategy would be to write a function that loops through each term, identifies the author associated with it and removes it if it is not associated with the author. I wrote an article about using get_terms to sort taxonomy terms by a custom order. If you take a look at the article, you can get an idea of how to utilize the get_terms filter.

The get_terms filter would also be useful in filtering out the terms on the taxonomy page. You must be careful, however, that you don’t misuse the filter. For instance, you should only ever apply the filter if a user is currently logged in, the user is an author, and the user is in the admin. As such, you will not affect the output for users viewing your site. Be very careful of this.

Hopefully this will point you in the right direction. This actually is a pretty cool idea and if I have some time, I may develop a plugin around it.

Leave a Comment