• Resolved henrylemmon

    (@henrylemmon)


    I have tried all the code in the codex like:

    `<?php
    if ( is_home()) {
    query_posts($query_string . ‘&cat=-17’ );
    }
    ?>

    it will exclude category, but seems to be resetting query also.

    please help

Viewing 5 replies - 16 through 20 (of 20 total)
  • This code worked perfect for me with one change:

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

    the ‘-‘ is not needed to remove the category, when the ‘-‘ was present it only displayed that category.

    Here is the exact code used:

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

    I’m guessing that the ‘exclude_category’ and the ‘-‘ were two opposites which caused it to filter all except cat 3.

    if I’m trying to apply the same code to a specific page how would I specify the page by name or ID instead of home.

    Sorry I take that back.
    I want to exclude categories after I click on a post on the blog page. So the post navigation (previous / next) needs to exclude posts of a specific category. BUT on another page the post navigation (previous / next) needs to exclude ALL other categories except the one I want (which was excluded from the blog page).

    casper14209

    (@casper14209)

    @frank
    This is actually unrelated to this thread and there is another thread that already exists.
    Can’t exclude a category from the previous_post_link and next_post_link
    also reference:
    Function Reference/previous post link
    Function Reference/next post link

    Thanks I found those but they don’t work, I’m sure it is a theme issue. I’m working on it from another angle.

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘How do I exclude category from blog page’ is closed to new replies.