Projects Widget Categories

I want the widget to display ONLY the categories I list in the widget editor, but it is showing the sub-categories of that category, and then a filter option so user can filter TO the category I want. How would I default it to that filtered state?

Parent Category
–Child Category
----GrandChild Category

When I put a project widget on my page, and limit it to the category ‘child-category’ it is showing the projects in that category PLUS both the grandchild and the great-grandchild projects. Is there a filter I can add to the SQL statement call in the widgets/fp-portfolio.php file to make it ONLY get the projects in the category that I’ve indicated, rather than including its children?

My page structure will be a project page for Parent, that has a widget on it to show all the ‘child’ categories. Then on the Child project page, it will have a widget to display all the Grandchildren, and so forth.

I’ve added this to my functions.php file in the child theme, but widget is still displaying child categories.

function wpse_filter_child_cats( $query ) {

if ( $query->is_category ) {
    $queried_object = get_queried_object();
    $child_cats = (array) get_term_children( $queried_object->term_id, 'category' );

    if ( ! $query->is_admin )
        //exclude the posts in child categories
        $query->set( 'category__not_in', array_merge( $child_cats ) );

    return $query;
add_filter( 'pre_get_posts', 'wpse_filter_child_cats' );

Hello there,

Please share a link to your page where you put the projects widget, so I can exactly get the situation you’re having.


Below the widget I have placed a screen shot of the hierarchy of categories and a screenshot of that particular widget selection. On this widget, I ‘only’ want the ‘Drawings & Models’ projects, but it is also showing the child and grandchild category projects.

I know that is the default behavior (to show category & all sub-categories) for category display when the select statement is ‘category_name’ - and if it could be a ‘category_in’ statement, it would exclude children, but that doesn’t work when we only have the name (slug) - so am looking for a function to exclude the children

I actually think the sample function I posted would work if I knew what this ‘if statement’ should say, because the page I’m using is not a ‘category’ page?

if ( $query->is_category ) {

Hello there,

Please log into your site admin area (/wp-admin) and check your project items which are displaying here in this page, and see if any parent category is assigned.


Yes, they all have parents assigned, as I show in the image I mentioned I posted on that same page - it is a screenshot showing the category structure from our admin. As I said before, the structure is

When I pick the slug for ‘child’ I only want projects posted to the ‘child’ category to show, but the widget is also showing the projects assigned to ‘grandchild’ and ‘great-grandchild’

This function works to exclude the children, like I want, in case others have this same need.

    add_action('pre_get_posts', 'filter_out_children');

    function filter_out_children( $query ) {
      if ( is_main_query() && $query->query_vars['post_type'] == 'projects' && ! is_admin() ) {
         $qo = $query->get_queried_object();
         $tax_query = array(
           'taxonomy' => 'category',
           'field' => 'id',
           'terms' => $qo->term_id,
           'include_children' => false
         $query->set( 'tax_query', array($tax_query) );