How to filter taxonomy terms properly o admin?
-
Hello! So right now I have an issue I’ve been dealing with for the past two days.
So basically, what I want to do is , I want to filter the category taxonomy metabox both on the post edit page , and the bulk edit screen (post listing), to display only childless categories.
The reason is , I am working on my category hierarchy and it is quite complex. But since my website allows multiple authors to log, post , and categorize their posts, I want them to categorize their products to the bottom-most categories possible only.
So far I’ve tried three approaches, and both have been unsuccesful for different reasons.
First approach: I used the get_terms filter to filter out the results of categories. And this works correctly on the post-edit page , and the post listing bulk edit section. The problem is that it also affects the taxonomy page, where I edit / create new categories, and me as an admin, I should be able to see the entirety of the category hierarchy on the taxonomy edit screen (This screen is not seen by authors, only me).
Second approach: I used the get_terms_args to filter the query params of the terms. This works perfectly both on the post-edit page, post-listing page, but same issue, it ALSO affects the taxonomy edit page.
What I notice from the first two approaches is that both have to do with the screen I am on. So I’ve tried to use the function get_current_screen() to limit how these filters affect the query, depending on the screen I am on. The problem is that the function throws a Fatal error on both filters, probably because at that point , no screen is defined. So I can’t make it work.
A third approach: I overrided the category meta_box_cb param to set my own metabox callback (Which is an exact copy of the default taxonomy WordPress one, but by overriding it, I get to alter the exact query from that metabox, which is what I want, and not ALL the queries from the admin. ) The problem is that meta_box_cb is not the same one as the post-list bulk screen, so even though on the post-edit screen it shows correctly, it’s incorrect on the post-list bulk screen.
One solution that just occurred to me is , perhaps I should with the “get_terms” approach, but ignore my filter if I am an admin. I am not completely happy with this approach because yes, I will be able to see the entire hierarchy from the taxonomy page, but I (as an admin) would also see the entire category tree from the post-edit page and the post-list bulk screen.
I know it sounds like I came to my own conclusion in the end (I am at this point assuming that either on the get_terms or the get_terms_args I will be able to do a get_current_user()) But I would really appreciate any input of people who’ve done this before or something similar. I can’t complain on the post-edit page since I can alter the metabox callback no problem. But there is literally no way for me to edit the post-list bulk screen. It seems to me there should be a filter there as well (which there isn’t).
Anyways, hopefully someone takes the time to read this and if you have any input that might help, I would really appreciate the help.
Thank you.
- The topic ‘How to filter taxonomy terms properly o admin?’ is closed to new replies.