• I am trying to exclude some posts from showing up in a category archive

    The code below I would have thought would exclude the post with id 35385 but it returns no posts at all on the archive. Removing the – in front of the 35385 returns only the post I am trying to exclude. So how come it does not work the other way around and how do I do it?

    function exclude_post( $query ) {
        if ($query->is_archive) {
            $query->set( 'p', -35385 );
            return;
        }//end if
    }//end function
    add_action( 'pre_get_posts', 'exclude_post' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,
    I have it done this way and it does work.

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

    oh, your trying to exclude single post, sorry.
    so maybe this way?

    function exclude_post( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'p', '-11' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_post' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude posts from Archive’ is closed to new replies.