Site logo

[Resolved] filter category on blog

Home Forums Support [Resolved] filter category on blog

Home Forums Support filter category on blog

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2516798
    Jusung

    Hello!

    I have made a couple of categories on my site.

    I don’t wanna show posts in the categories that starat with po and All.

    Is there a way to remove certain category in blog page?

    #2517231
    David
    Staff
    Customer Support

    Hi there,

    you can use the pre_get_posts hook in a PHP Snippet like so:

    add_action('pre_get_posts', 'exclude_category_posts');
    function exclude_category_posts( $query ) {
        if( $query->is_main_query() && !is_admin() && !is_singular() ) {
            $query->set('cat', array( -22, -33 ));
        }
    }

    In this line you set the IDs of the categories you want to exclude:

    $query->set('cat', array( -22, -33 )); make sure to keep the - minus before each Id.

    #2517905
    Jusung

    Thank you!! it works!

    #2518229
    David
    Staff
    Customer Support

    Glad to hear that!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.