Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #700723

    Ritchieb
    Participant

    Hello X-theme team,

    On my site i have three categories. One of them i don’t want to be shown. On your forum i saw the following lines which would do the trick:

    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( ‘cat’, ‘-1,-1347’ );
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_category’ );

    For me it didn’t work. I also tried to specify the actual page where the blog posts are on. I changed
    if ( $query->is_home() into if ($query->is_page(196)
    But also this didn’t work.

    Do you have other solutions?

    Thanks,
    Richard

    #700732

    Ritchieb
    Participant
    This reply has been marked as private.
    #700798

    Paul R
    Moderator

    Hi Richard,

    Thanks for writing in!

    I tried the code in my local test site and it seems to be working.

    You need to replace 1 and 1347 with the categoryid you want to exclude and since you only need one, you can enter just one number.

    eg.

    
    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-1' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
    

    Change 1 with your category id.

    Here is how to locate your category ids

    https://community.theme.co/kb/how-to-locate-category-ids/

    Hope that helps.

    #700934

    Ritchieb
    Participant

    Sorry i copy pasted the code from the forum and forgot to mention that i already had changed
    $query->set( ‘cat’, ‘-1,-1347’ );
    into my category’s id so:
    $query->set( ‘cat’, ‘-11’ );

    But the category (reviews) still shows.

    I als tried it with another of my categories (blogs; id 4) but also that category doesn’t disappear. And with every change, i first emptied the browsers cache.

    Plus all of sudden my wp adminbar is gone when viewing the site.

    I have checked Cornerstone and there i have “show wordpress toolbar” enabled
    And i also have checked the user settings under which i login and also there i have “show toobar while viewing site” enabled.

    PS: i left the jQuery untouched so you can see the current settings.

    Kind regards
    Richard

    #700936

    Ritchieb
    Participant
    This reply has been marked as private.
    #700940

    Ritchieb
    Participant

    A follow up to my message: you can forget the toolbar issue. I t has something to do with a maintenance plugin. For when i disable it, the toolbar is back. So i will have to sort that problem out at the other party.

    #701279

    Jade
    Moderator

    Hi Richard,

    Would you mind providing us with your FTP details so that we can safely revise and test the code?

    Thank you.

    #702281

    Ritchieb
    Participant
    This reply has been marked as private.
    #702308

    Paul R
    Moderator

    Hi,

    Thank you for providing your ftp login.

    Upon checking, I can see that you have added the code in the wrong place.

    Kindly remove the code in your custom > javascript box in the customizer that reads.

    
    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-11' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
    

    Then add it in your child theme’s functions.php file instead (wp-content/themes/x-child/functions.php).

    Hope that helps.

    #702604

    Ritchieb
    Participant

    Okay, i thought that all jQuery had to go in the javascript box in the customizer.

    I already have some jQuery there… should i move that over as well?

    And about the exclude jQuery: it partly worked. I now haven’t got the review posts (individually) anymore. But i still have them in the category list and in the archive list.
    See attached image. And i want them also gone overthere… Do i need an additional jQuery for that?

    thanks again
    Richard

    #702606

    Ritchieb
    Participant

    this is the image in which you can see reviews is still under categories

    #702977

    Darshana
    Moderator

    Hi there,

    You can use a plugin like Ultimate Category Excluder (https://wordpress.org/plugins/ultimate-category-excluder/) for this. Once you have installed the plugin, you can head over to Settings -> Category Exclusion section and then select the categories that you want to hide.

    Hope that helps.

    #703790

    Ritchieb
    Participant

    Hi there,

    i had already tried that but didn’t work. But i just tried again cause i couldn’t remember how the plugin settings were. But looking at it, there aren’t too many settings, you can’t either enable or disable categories. So i can hardly do anything wrong… hahaha but again: i doesn’t do anything! Maybe that is because the plugin isn’t updated for over two years.

    #703802

    Rue Nel
    Moderator

    Hello There,

    To exclude the review posts from your category and other archive pages, please update your code and use this code instead in your child theme’s functions.php file:

    function exclude_category( $query ) {
        if ( ( $query->is_home() || $query->is_archive() ) && $query->is_main_query() ) {
            $query->set( 'cat', '-11' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    We would loved to know if this has work for you. Thank you.

    #703825

    Ritchieb
    Participant

    No sorry, didn’t change a thing.

    In the mean time while searching the internet i stumbled upon a code which does the job but to a certain extend. I’m not a coder but maybe you guys (and girls?) understand this code and maybe can do some additions to it.

    I added the following code at the end of my child’s php file:

    add_filter( ‘widget_categories_args’, ‘wpsites_exclude_widget_category’, 11 );
    add_filter( ‘widget_categories_dropdown_args’, ‘wpsites_exclude_widget_category’, 11 );
    function wpsites_exclude_widget_category( $cat_args ) {
    $cat_args[‘exclude’] = array(’11’); //the categories removed
    return $cat_args;
    }

    Under categories, it’s gone… yeah!!! But the reviews posts are still under Archive.
    But as i said, you prolly understand the code and know how to add “archive” to it…. or is that too simple to say???? 😉

    best regards
    Richard