Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to wrap meta values seperated by comma in ? [closed]

If you are storing your meta values as a single string separated by commas, you can use the native PHP explode() to store them in an array:

$ct = get_post_meta($post->ID, 'ct', true);
// Store them in an array
$country_array = explode(',' , $ct);
// Run a loop and echo them
foreach( $country_array as $key => $country ){
    echo "<span>{$country}</span>";
    echo ( ( $key < ( count( $country_array ) -1 ) ) ? ', ':'' );
}

This might cause issue if you have a value like this:

Hong kong, China

Better yet save them as an array, instead of a single string.

Even better is to use custom taxonomy named country, and then use the_terms() to output them. This function allows you to add separator and before & after texts:

the_terms( $post->ID, 'country', '<span>', ',', '</span> );

Related Posts:

  1. How to break meta values into different items and avoid duplicates?
  2. Run a check for multiple meta key values
  3. IF Custom field value equals ZERO
  4. Update post meta custom field using block editor
  5. Set class if a meta value is set within post archive
  6. Custom Field: Display only if a specific key is selected outside the loop
  7. WP Query Args – search by meta_key or title
  8. Saving multiple custom meta box fields
  9. get Custom field label (select/dropdown) on front end
  10. Block Editor – Meta values not saved, meta changes to empty array on update
  11. Can I exclude a post by meta key using pre_get_posts function?
  12. What is the index [0] for on post meta fields?
  13. Custom field metabox not showing in back-end
  14. So much data in postmeta
  15. How to hide meta box values from custom fields list?
  16. get_post_meta() unserialize issue – returns boolean(false)
  17. What is the advantage of the wp_options design pattern?
  18. Meta keywords and descriptions plugin for manually editing meta for each page/post
  19. Up/Down voting system for WordPress
  20. post meta data clearing on autosave
  21. Create custom field on post draft or publish?
  22. Display info from custom fields in all images’ HTML
  23. Update meta values with AJAX
  24. Add a post meta key and value only if it does not exist on the post
  25. Custom fields to save multiple values
  26. Function to change meta value in database for each post
  27. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  28. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  29. Saving custom image meta fields
  30. Get all meta keys assigned to a post type
  31. How to sort category by custom field value
  32. trim custom field text value and show (…)
  33. Query post order by post and desc not working
  34. Adding custom fields (post meta) before/during wp_insert_post()
  35. How to query posts with certain custom meta data, and output Post data
  36. ACF: How to get the full field name (meta_key) by a field key?
  37. post meta getting deleted on save
  38. How to add a new meta key and assign timestamp to posts
  39. Custom field not updating when value is empty
  40. meta_compare seems to be treating values as strings instead of integers as expected
  41. Limit the number of acf content when displaying in post loop [closed]
  42. Read / Watch / Listen times – meta
  43. How to add custom metadata text box dropdown to sidebar in Gutenberg editor for all post types
  44. How do I Implement Atomic Update of Post Metadata?
  45. How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
  46. Displaying multiple URLs as custom field values
  47. Show values of custom post meta on ‘Add new post’ page?
  48. Custom post meta field effect on the performance on the post
  49. get_posts in meta box dropdown not showing latest posts
  50. Limits, not all post are showen when querying for posts by view count
  51. Display Custom Meta Box Field Only If Value is Present
  52. Ordering posts by custom field named “date” in backend
  53. Stop sending email everytime I updates my post
  54. Best way to store everyday post views?
  55. pre_get_posts : only get posts by wp_usermeta value
  56. Custom meta is not being saved
  57. Looping inside block return
  58. change order of images attached to post
  59. Sortable Custom Columns not sorting correct
  60. how can I show name and value of Custom Fields together?
  61. Can I save post meta programatically without setting metaboxes?
  62. Meta box values are displayed on Custom Fields list. Is it possible to hide them?
  63. Gravity Forms Update post meta from template [closed]
  64. Best way to achieve multiple links in a post title
  65. How can I add/update post meta in a admin menu page?
  66. Show array of meta_value in Edit Post Coloum
  67. custom filed from post in the side bar
  68. wordpress multi user question
  69. How to change text color depending on the number value (Using javascript)
  70. Adding Custom Metadata to my Archive/Posts page
  71. How to improve my non-unique metadata MySQL entries?
  72. Custom Fields Not Showing (ACF not installed)
  73. SQL query to change the value of a Custom Field
  74. Custom meta POST request fired twice when updating a post in Gutenberg
  75. Add custom field information to source meta data
  76. How do I update custom field post meta in frontend post template?
  77. How to Find List of Available Custom Fields for Theme?
  78. Create Shortcode shows only posts with custom_field meta
  79. Create Meta boxes dynamically
  80. Custom Fields Not Working In Footer
  81. ACF plugin and field update
  82. Delete custom meta
  83. Search one custom field?
  84. Can’t save meta field value if the title not set
  85. Adding custom fields to images
  86. How to save dynamically generated value in post type?
  87. wordpress simple post multi rating with post_meta and user_meta
  88. how to show this part only in the single post page?
  89. Display meta data from a custom field within plugin Category Grid View Gallery
  90. Custom fields / meta box output
  91. If post custom meta data is not set, show normal state
  92. Colecting values from custom field checkboxes and displaying them in the post
  93. Build Array from Input Fields question
  94. Display a list of posts whose meta field values are equal to the ID of the post being viewed?
  95. custom field with links
  96. How can i put a custom field inside this php
  97. Check for custom field value in different post type than current one and do something
  98. How to add new Metadata options (Date, Author, etc.) for Posts?
  99. Can I count every article following extracted meta value?
  100. What is the best way to get a different post’s custom field/postmeta with js?
Categories custom-field Tags custom-field, meta-value, post-meta
With the Yoast SEO plugin, is it possible to make the meta description a required form field?
How to display Unicode correctly?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress