Normally, in a WordPress post all tags are comma seperated: php, wordpress, functions.php. But what if you want to use a tag with commas in it? For instance “cafe, bar, restaurants“. Easy, create a filter in your WordPress functions.php, and display WordPress tags with commas in them.
Found on WordPress Development Stack Exchange.

To display commas in WordPress tags, open up your theme’s functions.php file, and add the following code:
// filter for tags with comma
// replace '--' with ', ' in the output - allow tags with comma this way
// e.g. save tag as "Fox--Peter" but display thx 2 filters like "Fox, Peter"
if(!is_admin()){ // make sure the filters are only called in the frontend
function comma_tag_filter($tag_arr){
$tag_arr_new = $tag_arr;
if($tag_arr->taxonomy == 'post_tag' && strpos($tag_arr->name, '--')){
$tag_arr_new->name = str_replace('--',', ',$tag_arr->name);
}
return $tag_arr_new;
}
add_filter('get_post_tag', 'comma_tag_filter');
function comma_tags_filter($tags_arr){
$tags_arr_new = array();
foreach($tags_arr as $tag_arr){
$tags_arr_new[] = comma_tag_filter($tag_arr);
}
return $tags_arr_new;
}
add_filter('get_terms', 'comma_tags_filter');
add_filter('get_the_terms', 'comma_tags_filter');
}
/*
* (code by “Andi” on WordPress Development Stack Exchange)
*/
Now when you save a tag with a double dash (--), the double dash will be displayed as a comma (,). This is an over 5 year old enhancement request on Trac: allow commas in tag names.
Extending the filter to display custom taxonomies comma separated
get_the_taxonomies() Retrieve all taxonomies associated with a post.
Jason, founder of Pretty Code Machine, asked a question in the Advanced WordPress Facebook group about how to display a comma separate custom taxonomy.
What he wanted to create is what the featured image shows: saving one taxonomy Locations “Brattleboro, VT” and not two separate Locations taxonomies (“Brattleboro”, “VT”).
I replied and pointed him to this post and filter mentioned above.
After we had some further contact back and forth, Jason came up with the following code to include a taxonomy filter to display the comma separated custom taxonomy for Locations he needed:
<?php
// ...
// filter for tags (as a taxonomy) with comma
// replace '--' with ', ' in the output - allow tags with comma this way
if( !is_admin() ) { // make sure the filters are only called in the frontend
$custom_taxonomy_type = 'location'; // here goes your taxonomy type
function comma_taxonomy_filter( $tag_arr ){
global $custom_taxonomy_type;
$tag_arr_new = $tag_arr;
if( $tag_arr->taxonomy == $custom_taxonomy_type && strpos( $tag_arr->name, '--' ) ){
$tag_arr_new->name = str_replace( '--' , ', ', $tag_arr->name);
}
return $tag_arr_new;
}
add_filter( 'get_' . $custom_taxonomy_type, comma_taxonomy_filter );
function comma_taxonomies_filter( $tags_arr ) {
$tags_arr_new = array();
foreach( $tags_arr as $tag_arr ) {
$tags_arr_new[] = comma_taxonomy_filter( $tag_arr );
}
return $tags_arr_new;
}
add_filter( 'get_the_taxonomies', 'comma_taxonomies_filter' );
add_filter( 'get_terms', 'comma_taxonomies_filter' );
add_filter( 'get_the_terms', 'comma_taxonomies_filter' );
}
Pretty neat, isn’t it?! 🙂
Summary
- To display WordPress tags with commas, you can create a filter in your theme’s functions.php file.
- Using a double dash (–) allows you to save tags with commas, which is a solution to a long-standing enhancement request.
- You can extend this filter to display custom taxonomies as comma-separated, like saving ‘Brattleboro, VT’ as one location.
- This solution helps maintain clarity and organization in your WordPress tags and taxonomies.