I found the code below that exclude a child category from it’s parent’s archive, but the child category is still present in “grand parent” archive. I have a category listed as: News, European Union, Approved legislation from Parliament. The last category is just an auto updated category that should not be listed on front page (handled through interface) nor in “News” section or “European Union” section. How would I be able to achieve this, and also for future reference: how to nest these kind of issues indefinitely?
Grand parent’s category number is 3,
Parent’s category number is 1169 and
child’s category number is 2184.
function exclude_categories() {
$excluded = array( ‘2184’ );
if (is_category(1169)){
set_query_var( ‘category__not_in’, $excluded );
}
}
add_filter( ‘pre_get_posts’, ‘exclude_categories’ );
Thanks =)
And by the way. The attached code doesn’t work properly. It leaves child posts in parent.
Hello 24Uutiset,
Please notice that if you want to exclude some categories from your block, you can do this from Block Settings, like this -> https://www.screencast.com/t/uOkDxhr8 For more information about Block Settings, please consult our documentation here -> https://forum.tagdiv.com/block-settings-guide/ If is not what you mean and if this task will involve some additional customization through the code and if you do not have a better coding skills to do this, please consider hiring a freelancer/developer to help you because we cannot provide custom work at the moment, sorry!
Hope this helps!
Thanks for your understanding!
That is not my issue.
I’m referring to this post answered by Andrei: https://forum.tagdiv.com/topic/categories-6/
where child posts are visible in the archives. When you click in on a sub heading on site: http://eu-uutiset.fi/wp-content/uploads/2017/03/categor_parent.jpg
then it would be nice that the child posts of that sub heading isn’t visible, like it is now:
http://eu-uutiset.fi/wp-content/uploads/2017/03/categor_parent_fail.jpg
According to Andrei the following code should have worked:
function exclude_categories() {
$excluded = array( ‘2184’ );
if (is_category(1169)){
set_query_var( ‘category__not_in’, $excluded );
}
}
add_filter( ‘pre_get_posts’, ‘exclude_categories’ );
and it works partly. This is from parent:
http://eu-uutiset.fi/wp-content/uploads/2017/03/categor_parent_fail.jpg
it shows that the child posts are visible, but sowehow they have been placed further down on than without that code.
Hello 24Uutiset,
I have inspected your website and I saw that you have some additional customizations, sorry! Please try to revert the theme to the default state and try again the code. I have tested on my side and seems to work as expected. If the problem is still there, please send us an email at contact@tagdiv.com with your log-in information (wp-admin) so we can further inspect it closer your setup page. Provide there the link to this topic so we can easily identify you.
Thanks for your understanding!