Hello, I’m currently using the following code to hide specific categories from displaying in the meta of my posts:
//* Remove categories from post meta
add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){
$exclude = array(43, 44);
if (!is_admin()) {
foreach($terms as $key => $term){
if(in_array($term->term_id, $exclude)) unset($terms[$key]);
}
}
return $terms;
}
But I’ve realised this is causing issues when I edit and update posts as it takes the categories off completely. Any solutions to this?
Hey tomhilditch,
Can you try to put:
$exclude = array(43, 44);
inside the if statement:
if (!is_admin()) {
Hope it helps.
Best regards,
Nikko
Hi Nikko,
I just tried
add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){
if (!is_admin()) {
$exclude = array(43, 44);
foreach($terms as $key => $term){
if(in_array($term->term_id, $exclude)) unset($terms[$key]);
}
}
return $terms;
}
But it’s still causing the same problem. Tried wiping cache after doing this and it still doesn’t work
Hi tomhilditch,
I have tested the code, and though upon saving it visually removes the excluded categories but if you refresh the page it doesn’t actually remove it unless you uncheck the category and save.
I tried to look for other conditional tags that may apply but I could find none, I have tested this code with default wordpress themes and the effect is the same.
Best regards,
Nikko