- This topic has 9 replies, 2 voices, and was last updated 2 years, 1 month ago by
Elvin.
-
AuthorPosts
-
April 11, 2018 at 9:27 am #547523
dale
Hi! I have a couple categories that I only use for organization on the back end and don’t want visible on the front end—meaning I’d like to hide them from the meta area that appears below each post. I tried this, where “feature” and “10” are standins for the category:
function the_category_filter($thelist,$separator=’ ‘) {
if(!defined(‘WP_ADMIN’)) {
//list the category names to exclude
$exclude = array(‘feature’);
$cats = explode($separator,$thelist);
$newlist = array();
foreach($cats as $cat) {
$catname = trim(strip_tags($cat));
if(!in_array($catname,$exclude))
$newlist[] = $cat;
}
return implode($separator,$newlist);
} else
return $thelist;
}
add_filter(‘the_category’,’the_category_filter’,10);`pasted into Code Snippets and activated, but it didn’t work. Thoughts?
April 11, 2018 at 8:53 pm #547949Tom
Lead DeveloperLead DeveloperPerhaps this plugin would be helpful?: https://en-ca.wordpress.org/plugins/ultimate-category-excluder/
April 12, 2018 at 8:40 am #548534dale
Thanks for your response.
That plug-in hides actual posts. I am just looking to hide the name of the category from the meta that appears beneath each post.
For example, on page https://www.dalecameronlowry.com/submission-call-fairy-tales-about-summer-nights-pays-20-due-may-20/, I have two categories showing in the meta at the bottom of the page: “Calls for Submissions, For Writers”
let’s say that I don’t want readers clicking on “For writers” because it’s only used for backend organization. So I want to keep the post in that category, but have the meta only read “Calls for Submissions”
Is that clearer?
April 12, 2018 at 8:36 pm #548939Tom
Lead DeveloperLead DeveloperAh, gotcha.
Untested, but you could try this filter:
add_filter( 'get_terms', 'tu_remove_terms' ); function tu_remove_terms( $terms ) { unset( $terms['category_slug'] ); return $terms; }
April 13, 2018 at 6:22 am #549322dale
Thanks. I added it like this to Code Snippets:
add_filter( 'get_terms', 'tu_remove_terms' ); function tu_remove_terms( $terms ) { unset( $terms ('for-writers') ); return $terms; }
But nothing happened. I looked in incognito and purged the site-side caches as well. Did I adapt it incorrectly?
April 13, 2018 at 10:38 am #549669Tom
Lead DeveloperLead DeveloperHmm, should have worked.
Let’s try this function: https://codex.wordpress.org/Function_Reference/wp_remove_object_terms
wp_remove_object_terms( get_the_ID(), 'for-writers', 'category' );
April 13, 2018 at 1:35 pm #549815dale
That doesn’t seem to work either. I’ll keep looking for solutions.
April 13, 2018 at 9:04 pm #549980Tom
Lead DeveloperLead DeveloperHmm, strange. It may be worth asking on stackoverflow.com. Someone may have done this before.
You could use tags instead of categories for the internal tags. Then just choose not to display tags on the frontend.
April 21, 2021 at 4:06 am #1742920Lorenzo
I neded the same and It works in my case with this code:
add_filter('get_the_terms', 'hide_categories_terms', 10, 3); function hide_categories_terms($terms, $post_id, $taxonomy){ $exclude = array(IDcat1,IDcat2...); if (!is_admin()) { foreach($terms as $key => $term){ if(in_array($term->term_id, $exclude)) unset($terms[$key]); } } return $terms; }
April 21, 2021 at 4:00 pm #1743820Elvin
StaffCustomer SupportHi Lorenzo,
Thank you for sharing it with us. 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.