1

I have a custom post type in my theme called portfolio. It's categories are called project-categories. I am trying to exclude a project category for logged in users. I cannot get this to work at all. I have read through the pre_get_posts codex but I am just stuck. I'm not sure if I am defining the categories for my custom post type or if it would be the same as a normal post category. When I click on the category to edit it, this is in the browser url bar

wp-admin/edit-tags.php?action=edit&taxonomy=project-type&tag_ID=94&post_type=po‌​rtfolio

Here is the code I'm trying right now.

function exclude_category( $query ) {
    if ( is_user_logged_in() && $query->is_main_query() ) {
        $taxquery = array(
        'taxonomy' => 'project-type',
        'field' => 'id',
        'terms' => array( 94 ),
        'operator' => 'NOT IN'
    );

        $query->set( 'tax_query', $taxquery);
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

Any ideas on what I might be doing wrong would be greatly appreciated. Thanks

1 Answer 1

1

OK, so you have few mistakes in there.

  1. field can be term_id or slug, but not id.
  2. tax_query should be array of arrays and not an array.

So here is fixed code:

function exclude_category( $query ) {
    if ( is_user_logged_in() && $query->is_main_query() ) {
        $taxquery = array(
            array(
                'taxonomy' => 'project-type',
                'field' => 'term_id',
                'terms' => array( 94 ),
                'operator' => 'NOT IN'
            ),
        );

        $query->set( 'tax_query', $taxquery);
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

It's not tested, but I'm pretty sure it should work just fine.

3
  • Hey Krzysiek, thanks for the response. I tried the code and it's still not hiding the category. I have tried just about everything I can think of. I think it's something going on with the theme that is stopping it from working. If I send you the wordpress zip and sql of a fresh install with a simple portfolio would you be able to take a look at it?
    – Musik8101
    Commented Apr 29, 2014 at 9:05
  • Hmm, are you sure that this theme is using native wp_query? Maybe it's creating its own query in this template. Commented Apr 29, 2014 at 9:26
  • no I'm not sure, I've contacted support but their responses are pretty slow. Would this be it? I found it in the functions.php $portfolio = array( 'post_type' => 'portfolio' ); $the_query = new WP_Query($portfolio);
    – Musik8101
    Commented Apr 29, 2014 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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