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 members. My first question is, would I be able to reference the project-categories the same as a normal post category?
This is the code I have so far which is not working. I have read through the codex but I think I am missing something. pre_get_posts Codex
function exclude_category( $query ) {
if ( is_user_logged_in() && $query->is_main_query() ) {
$query->set( 'cat', '-94');
}
}
add_action( 'pre_get_posts', 'exclude_category' );
Here are the Taxonomy for the custom post type
#-----------------------------------------------------------------#
# Taxonomy attached to portfolio
#-----------------------------------------------------------------#
$category_labels = array(
'name' => __( 'Project Categories', NECTAR_THEME_NAME),
'singular_name' => __( 'Project Category', NECTAR_THEME_NAME),
'search_items' => __( 'Search Project Categories', NECTAR_THEME_NAME),
'all_items' => __( 'All Project Categories', NECTAR_THEME_NAME),
'parent_item' => __( 'Parent Project Category', NECTAR_THEME_NAME),
'edit_item' => __( 'Edit Project Category', NECTAR_THEME_NAME),
'update_item' => __( 'Update Project Category', NECTAR_THEME_NAME),
'add_new_item' => __( 'Add New Project Category', NECTAR_THEME_NAME),
'menu_name' => __( 'Project Categories', NECTAR_THEME_NAME)
);
register_taxonomy("project-type",
array("portfolio"),
array("hierarchical" => true,
'labels' => $category_labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'project-type' )
));
$attributes_labels = array(
'name' => __( 'Project Attributes', NECTAR_THEME_NAME),
'singular_name' => __( 'Project Attribute', NECTAR_THEME_NAME),
'search_items' => __( 'Search Project Attributes', NECTAR_THEME_NAME),
'all_items' => __( 'All Project Attributes', NECTAR_THEME_NAME),
'parent_item' => __( 'Parent Project Attribute', NECTAR_THEME_NAME),
'edit_item' => __( 'Edit Project Attribute', NECTAR_THEME_NAME),
'update_item' => __( 'Update Project Attribute', NECTAR_THEME_NAME),
'add_new_item' => __( 'Add New Project Attribute', NECTAR_THEME_NAME),
'new_item_name' => __( 'New Project Attribute', NECTAR_THEME_NAME),
'menu_name' => __( 'Project Attributes', NECTAR_THEME_NAME)
);
register_taxonomy('project-attributes',
array('portfolio'),
array('hierarchical' => true,
'labels' => $attributes_labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'project-attributes' )
));
I am not getting any errors but the category is not being excluded when I am logged in.
Any help would be greatly appreciated.
Thanks
Update:
I have also tried These with no luck
function exclude_category( $query ) {
if ( is_user_logged_in() && $query->is_main_query() ) {
$taxquery = array(
array(
'post_type' => 'portfolio',
'taxonomy' => 'project-type',
'field' => 'id',
'terms' => array( 94 ),
'operator' => 'NOT IN'
)
);
$query->set( 'tax_query', $taxquery);
}
}
add_action( 'pre_get_posts', 'exclude_category' );
And this as well
function exclude_category( $query ) {
if ( is_user_logged_in() && $query->is_main_query() ) {
$taxquery = array(
array(
'post_type' => 'portfolio',
'taxonomy' => 'project-type',
'field' => 'slug',
'terms' => 'preview',
'operator' => 'NOT IN'
)
);
$query->set( 'tax_query', $taxquery);
}
}
add_action( 'pre_get_posts', 'exclude_category' );
add tax_query arguments to it.
For some reason it was not working. I couldn’t figure out if the theme was creating a new query or what. So here’s the logic I used to resolve this. I gave mark the solution because technically it was the solution to the issue asked but here’s how I approached my issue.
After not being able to solve the problem through the wp query I decided that since the content was already protected using the groups plugin all I needed to do was exclude the Preview category from my original portfolio and then create a Preview portfolio to add to my site navigation. From there I could then hide the Preview navigation link from logged in users and then the real portfolio would already be hidden from users not logged in with the groups plugin. Here’s how I achieve it.
I added the previews portfolio to my main menu by doing a custom link. I selected the css class screen option and added the class logged-in-nav to the preview menu link.
Then I added this css to my theme to hide the previews from logged in users
Change out your-nav-name with whatever your menu name is in your source code.
Then all I did was secure my portfolio section with the groups plugin so the original portfolio link doesn’t show to non-logged in users. So users not logged in see the Previews link and do not see the original one and logged in see the Original Portfolio and do not see the preview link in the nav.
This is just an example of the logic on how you can restrict from logged in users through wordpress. Hope it helps anyone trying to work through a similar issue.