I am trying to hide a custom post type category to logged in users with Pre_Get_Posts


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


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

, , , , Musik8101 8 months 0 Answers 75 views 0

Leave an answer