WordPress.org

Forums

exclude multiple category id , how to code it (2 posts)

  1. boson600
    Member
    Posted 1 year ago #

    dear,

    i want to exclude both category id = 40 and id =162 posts, but below code doesn't work, it only excludes category id=40 posts, anyone can modify the code for me? thanks alots.

    $args = array( 'numberposts' => 3, 'category' => -40,-162 );

  2. WP Sites - Brad Dalton
    Member
    Posted 1 year ago #

    Add this in your child themes functions.php

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

    Source http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Exclude_categories_on_your_main_page

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags