0

I have two links in

  • "news" where all the news posts goes
  • "blog" where the rest goes

just wanted to exclude the category "news" in the blog page

I use the code

$news = new WP_query ('category_name =-news')

and it is not working

Essentially, I want tell wordpress, I want all the posts to go into the "blog" page but not in the post that is categorize as "news"

1

In your functions.php file, use pre_get_posts and swap out the -1 in the following code with the category I.D you want to exclude from your posts page.

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

Use the parameters cat or category__not_in:

$query = new WP_Query( 'cat=-1234' );

The minus means to exclude, the ID is used.

$query = new WP_Query( array( 'category__not_in' => array( 1234 ) ) );

Uses the ID too.

Use get_cat_ID() to get the ID by name or get_category_by_slug() to do it by slug, the latter returns an object though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.