Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been able to restrict users to only seeing their own posts, both on the front end and within the admin section. Now I would like to restrict them to only seeing the Custom Taxonomy data they have entered.

My example: I have created a Custom Taxonomy called Partners. I would like each user to be able to add his or her partners so they can be associated with a post. I want to keep a User's Partners only available to the User who created them.

Does that make sense? Does anyone have a way to implement this?

Thanks!

share|improve this question

2 Answers 2

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.

share|improve this answer

Wow! Awesome response. Thanks. I get the concept for sure. I will read your articles and probably do some more associated research. I can tweak existing PHP and even write a (very) little so this may be beyond my skill level but I'm down for trying.

If you ever get a plugin together let me know. I think I'm not the only one who could make use of it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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