Hide Items in a Term From a Post Type Archive
Keeps items filed under certain terms (such as "Archived" or "Internal") off a post type's main archive, while their own term archive still lists them.
Opens WPIDE on
Code
// Post type archive, taxonomy and term slugs to hide.
$wpide_hide_terms = array(
'post_type' => 'job',
'taxonomy' => 'job_type',
'terms' => array( 'internal' ),
);
add_action( 'pre_get_posts', function ( $query ) use ( $wpide_hide_terms ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_post_type_archive( $wpide_hide_terms['post_type'] ) || ! taxonomy_exists( $wpide_hide_terms['taxonomy'] ) ) {
return;
}
$tax_query = (array) $query->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => $wpide_hide_terms['taxonomy'],
'field' => 'slug',
'terms' => $wpide_hide_terms['terms'],
'operator' => 'NOT IN',
);
$query->set( 'tax_query', $tax_query );
} );
Notes
Set the post type, taxonomy and term slugs.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.