Exclude child categories form category filter?
Hi, is it possible to exclude child categories from the categories filter in the post list?
thank you,
Els
Hi Els,
The default category column filter drop down is one of the few filters that we don’t manage with our plugin. This specific filter is added by WordPress itself and if I’m correct it rendered by calling wp_dropdown_categories
. I think they have some hooks that you can use to alter the items in the drop down.
Another approach would be to use our Taxonomy column and use one of our hooks to only show parent categories.
The hook would look something like this.
function acp_change_category_filter_options_args( $args ) {
$args['parent'] = 0;
return $args;
}
add_filter( 'acp/filtering/terms_args', 'acp_change_category_filter_options_args', 2, 1 );
There is no way to alter this in a setting in the column, so you must know how to alter code and hooks in your theme.
Hi Stefan, thanks for your quick response. I wasn’t clear enough in my question though … what I meant was that I want to see all categories in the post category filter but in the result I only want to see the posts that are in that specific category and not also all posts in the child categories of the chosen category. Is it possible to alter that?
thank you,
Els
Ah, I see what you mean now.
Unfortunately, this is not possible with our plugin.
To accomplish that, you’ll have to hook into the WP_Query yourself and alter the tax_query params yourself.
We don’t set the include_children
argument which means it is set to true
by default. We don’t have a filter to alter this behavior for a specific filter.
Hi Stefan, thanks for this info … after some Googling based on your keywords I tried this …
function exclude_child_taxonomies( $query ) {
if ( is_admin() && $query->is_main_query() ) {
if (is_tax('category')) {
$tax_query = $query->tax_query->queries;
$tax_query[0]['include_children'] = 0;
$query->set( 'tax_query', $tax_query );
}
}
return;
}
add_action( 'pre_get_posts', 'exclude_child_taxonomies', 1 );
It doesn’t work yet but before I keep digging, can I know what column set is active so that I can switch between included and excluded?
thanks a lot and greetings,
Els
Yes, this is possible with both the default Category column and our taxonomy column.
You both have to read it from the URL parameters
1) Default category column
In this case, you’ll find always cat={termid} in the URL so you could use
$term = filter_input( INPUT_GET, 'cat' );
2) Taxonomy column
Our column works on the tax_query so your solution could work here. But the function you posted here, assumes the tax_query only has one taxonomy argument. I advise that you loop through the tax_query and look for a tax query argument where taxonomy equals the taxonomy you want to change. Besides this, I think that probably want to try to fix it with this column instead of the default category column since for this column you’re sure that the tax_query contains the information that you need.
Hi Stefan, thanks again for you answer … I’m trying to figure out what to do based on your info and found this …
function taxonomy_archive_exclude_children($query){
$taxonomy_slugs = ['category'];
if(is_admin() && $query->is_main_query() && is_tax($taxonomy_slugs)){
foreach($query->tax_query->queries as &$tax_query_item){
if(empty($taxonomy_slugs) || in_array($tax_query_item['taxonomy'], $taxonomy_slugs)){
$tax_query_item['include_children'] = 0;
}
}
}
}
add_action('parse_tax_query', 'taxonomy_archive_exclude_children');
I’m not sure what to change to get it working with your taxonomy column (doesn’t work yet anyhow). I would really appreciate your answer but understand if you’ll stop helping me out. :-)
I have not really understood the code … is this the code behind the filter button on the posts list in the admin? So, a query to get posts that has the taxonomy ‘category’ set as filter and with this code I alter the ‘category’ filter part to exclude children? Do you know a source maybe where I can immediately find the source code behind admin controls, that would be really useful.
thanks a lot and greetings,
Els
Well, I know this snippet is not the code behind the filter button but a hook into that code and is my if (is_admin() && $query->is_main_query() && is_tax($taxonomy_slugs) the right check?
thanks,
Els
Hi Els,
To be honest, I think tha the is_tax() check is not working since you’re using probably our taxonomy column as I suggested. I made a snippet for you, that should work if you’re using the taxonomy column. What it does, is that it checks for every item in the tax query instead of just targetting the first item. It now checks for the category taxonomy, so the alteration only applies to that tax query.
Waaw, I’m so grateful! (Just so you know … changed the ‘false’ to ‘0’.) I just posted a review on wordpress.org to compliment on your plugin and awesome support. Don’t know if more people are interested in this feature but it could become an acp checkbox of the taxanomy columm maybe? Anyway, thank you soooo much for helping me out here.
greetings,
Els
You’re welcome!
Thank you very much for your positive review on WordPress.org, much appreciated!
I will close this topic.
You must be logged in to reply to this topic.