• 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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • MK

    (@mkarimzada)

    Hi there-
    I like your third approach it seems easier to implement and what you could do is create a custom column on posts table and add support for filtering the posts.

    However, it worths to note that custom meta boxes feels a little slower and less functional with the block editor. I’ve done something similar in the past, where I wanted to create a custom meta query for posts with different priorities in categories archive, similar to sticky posts but with more complex meta query to filter them. What I ended up doing was creating a setting sidebar block and load categories via REST API and conditionally send back query results to pre_get_posts hook.

    I hope this helps!

    Thread Starter mauricionieto1994

    (@mauricionieto1994)

    @mkarimzada thank you for your input. To give more context, I am working with the product category taxonomy of Woocommerce, so I am already using the old editor (not gutenberg) so this should not be an issue.

    I ended up going this route, because I found the quick_edit_show_taxonomy filter which allows me to hide the product category taxonomy from the post-list bulk edit and quick edit. This was much cleaner because with both get_terms_args and get_terms I felt I didn’t have exact control of which query I was filtering (Through error_logging and debugging, my filter would fire abount fifteen times! ) So by simply editing the metabox callback and hiding the quick edit box, I fixed what I wanted to do.

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to filter taxonomy terms properly o admin?’ is closed to new replies.