Join the Soledad Facebook Users Group here
for Share, assistance, discussion, and Updates related to Soledad WordPress Theme.

If you can't create a new ticket - you can send us an email via our contact form and we will get back to you.

Okay
  Public Ticket #2874850
Avoid post from child categories
Closed

Comments

  • fioridizuccaimmagine started the conversation

    Hi, I've noticed that in a parent category I see also posts from child categories.

    Is it a bug? How I can disable this?

  •   fioridizuccaimmagine replied privately
  •  2,610
    PenciDesign replied

    Hi,

    No. This is not a bug. Your post will show all categories you've selected on the post editor.

    Regards,
    PenciDesign.


  •   fioridizuccaimmagine replied privately
  •  2,610
    PenciDesign replied

    Hi,

    Please send me your site credential in a PRIVATE reply, I'll edit a demo post for you.

    Regards,
    PenciDesign.


  • fioridizuccaimmagine replied

    Actually I'm working on it in a local path.

    I didn't understand if you got the problem,

    I have a parent category "cat1" and inside two child categories "cat1-1" and "cat1-2",

    the post will have as categories cat1-1 and cat1-2,

    so the question is "that post will be visible in categories page like cat1-1 and cat1-2 or also in cat1 list posts?

  •  2,610
    PenciDesign replied

    Hi,

    If you want this post doesn't visible in the parent cat (cat1), please do not select this category while editing a post.

    image.png

    Regards,
    PenciDesign.


  •   fioridizuccaimmagine replied privately
  •  2,610
    PenciDesign replied

    Hi,

    Please go to your Dashboard → Tools → Export, choose All content, then upload the export file here. I need to check your post settings.

    Regards,
    PenciDesign.


  •   fioridizuccaimmagine replied privately
  •  2,610
    PenciDesign replied

    Hi,

    This file is still not enough data to debug your site. Can I check your site via TeamViewer?

    Regards,
    PenciDesign.


  •   fioridizuccaimmagine replied privately
  •   PenciDesign replied privately
  • fioridizuccaimmagine replied

    Thanks, I put it in child theme functions file and now it is ok.

  •   fioridizuccaimmagine replied privately
  • fioridizuccaimmagine replied

    It seems that this code works well, what do you think about?


    if ( !function_exists('fb_filter_child_cats') ) {
        function fb_filter_child_cats( $cats ) {
            global $wp_query, $wpdb;
            if ( is_category() ) {
                // get children ID's
                if ( $excludes = get_categories( "child_of=" . $wp_query->get('cat') ) ) {
                    // set array with ID's
                    foreach ( $excludes as $key => $value ) {
                        $exclude[] = $value->cat_ID;
                    }
                }
                // remove child cats
                if ( isset($exclude) && is_array($exclude) ) {
                    $cats .= " AND " . $wpdb->prefix . "term_relationships.term_taxonomy_id NOT IN (" . implode(",", $exclude) . ") ";
                }
            }
            return $cats;
        }
        if ( !is_admin() ) {
            add_filter( 'posts_where', 'fb_filter_child_cats' );
        }
    }


  •  2,610
    PenciDesign replied

    Hi,

    Yes. This code also works by the concept you want. I'll find another solution with the best performance then update this ticket soon.

    Regards,
    PenciDesign.


  • fioridizuccaimmagine replied

    Hi, the code I used to exclude posts of child categories in parent categories doesn't work anymore with wordpress 6. 

    Actually it shows me an empty parent category .

    Can you tell me if there is a way to see in my categories only post that belong to that category with seeing the ones of child categories?

  •  2,610
    PenciDesign replied

    Hi,

    Please modify the PHP code like this:

    if ( !function_exists('fb_filter_child_cats') ) {
        function fb_filter_child_cats( $cats ) {
            global $wp_query, $wpdb;
            if ( is_category() ) {
                // get children ID's
                $catID = (int) $wp_query->get('cat');
                if ( $excludes = get_categories( [ 'child_of' => $catID ] ) ) {
                    // set array with ID's
                    foreach ( $excludes as $key => $value ) {
                        $exclude[] = (int) $value->cat_ID;
                    }
                }
                // remove child cats
                if ( isset($exclude) && is_array($exclude) ) {
                    $cats .= " AND " . $wpdb->prefix . "term_relationships.term_taxonomy_id NOT IN (" . implode(",", $exclude) . ") ";
                }
            }
            return $cats;
        }
        if ( !is_admin() ) {
            add_filter( 'posts_where', 'fb_filter_child_cats' );
        }
    }

    Regards,
    PenciDesign.



  • weslei replied

    Good morning, I have a problem with the categories, like before the categories and subcategories were separated right, in the mobile and tablet versions the menu is correct but in the PC version when I place the mouse pointer on top it shows all the categories and subcategories I created .
    
    



  •  2,610
    PenciDesign replied

    Hi @weslei,

    Please create a new private ticket with your WordPress admin account.

    I'll check and help you.

    Regards,
    PenciDesign.