vmehra88
vmehra88

Reputation: 31

Wordpress: Using post__not_in to exclude custom taxonomy category

I'm trying to exclude the items within a custom taxonomy category (slug: private-case-study, ID: 5) from a loop. I'm using the following code, does anyone have suggestions or ideas why it's not working? Thanks in advance!


    // Get the current page ID
        $this_post = $post->ID;
        $private_case_study = get_term_by('slug', 'private-case-study', 'mgt_portfolio_filter');


        // Show items from specific category
        if($category_name == '') {
            $wp_query = new WP_Query(array(
                'post_type' => 'mgt_portfolio',
                'posts_per_page' => $posts_per_page,
                'orderby'    => $orderby,
                'order' => $order,
                'post__not_in' => array($this_post, $private_case_study)
            ));
        } else {
            $wp_query = new WP_Query(array(
                'post_type' => 'mgt_portfolio',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'mgt_portfolio_filter',
                        'field'    => 'slug',
                        'terms'    => $category_name,
                    ),
                ),
                'posts_per_page' => $posts_per_page,
                'orderby'    => $orderby,
                'post__not_in' => array($this_post, $private_case_study),
                'order' => $order
            ));
        }

Upvotes: 1

Views: 2857

Answers (2)

Rockstar564
Rockstar564

Reputation: 21

//Get the current page ID $this_post = $post->ID; $private_case_study = get_term_by('slug', 'private-case-study', 'mgt_portfolio_filter');

    // Show items from specific category
    if($category_name == '') {
        $wp_query = new WP_Query(array(
            'post_type' => 'mgt_portfolio',
            'posts_per_page' => $posts_per_page,
            'orderby'    => $orderby,
            'order' => $order,
            'post__not_in' => array($this_post)
        ));
    } else {
        $wp_query = new WP_Query(array(
            'post_type' => 'mgt_portfolio',
            'tax_query' => array(
                array(
                    'taxonomy' => 'mgt_portfolio_filter',
                    'field'    => 'slug',
                    'terms'    => $category_name,
                ),
            ),
            'posts_per_page' => $posts_per_page,
            'orderby'    => $orderby,
            'post__not_in' => array($this_post),
            'order' => $order
        ));
    }

Upvotes: 2

mburesh
mburesh

Reputation: 1028

post__not_in takes an array of IDs and excludes them. You want to being using category__not_in which takes an array of category IDs and excludes them.

Take a look at the documentation.

Upvotes: 0

Related Questions