Another explanation:
For example, the post to get related posts for has tags with ids 1,2,3,4,5 assigned to it. If you exclude term ids 1,2 and 3 the plugin will get (related) posts that also have tag 3 or 4 or 5. Posts are ordered by how many of the tags (3,4,5) they have in common. The related posts found can however still have tag ids 1,2 assigned to it.
You can have it exclude posts that have any of the excluded terms with this in your theme’s functions.php file:
add_filter( 'related_posts_by_taxonomy_shortcode_atts', 'rpbt_exclude_terms_strict' );
function rpbt_exclude_terms_strict( $atts ) {
if ( !( isset( $atts['exclude_terms'] ) && $atts['exclude_terms'] ) ) {
return $atts;
}
// use all taxonomies to get the excluded terms
$taxonomies = get_taxonomies( array( 'public' => true ), 'names', 'and' );
$terms = get_terms( $taxonomies, array( 'include' => $atts['exclude_terms'] ) );
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return $atts;
}
// use the taxonomies from the terms
$exclude_taxonomies = array_unique( wp_list_pluck( $terms , 'taxonomy' ) );
$tax_query = array();
foreach ( $exclude_taxonomies as $tax ) {
$tax_query[] = array(
'taxonomy' => $tax,
'field' => 'id',
'terms' => $atts['exclude_terms'],
);
}
$tax_query['relation'] = 'OR';
$args = array(
'fields' => 'ids',
'posts_per_page' => -1,
'tax_query' => $tax_query
);
$posts = get_posts( $args );
if ( $posts ) {
// exclude posts that have terms you want to exclude
$atts['exclude_posts'] = $posts;
}
return $atts;
}
btw:
consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.