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:
- How to break meta values into different items and avoid duplicates?
- Run a check for multiple meta key values
- IF Custom field value equals ZERO
- Update post meta custom field using block editor
- Set class if a meta value is set within post archive
- Custom Field: Display only if a specific key is selected outside the loop
- WP Query Args – search by meta_key or title
- Saving multiple custom meta box fields
- get Custom field label (select/dropdown) on front end
- Block Editor – Meta values not saved, meta changes to empty array on update
- Can I exclude a post by meta key using pre_get_posts function?
- What is the index [0] for on post meta fields?
- Custom field metabox not showing in back-end
- So much data in postmeta
- How to hide meta box values from custom fields list?
- get_post_meta() unserialize issue – returns boolean(false)
- What is the advantage of the wp_options design pattern?
- Meta keywords and descriptions plugin for manually editing meta for each page/post
- Up/Down voting system for WordPress
- post meta data clearing on autosave
- Create custom field on post draft or publish?
- Display info from custom fields in all images’ HTML
- Update meta values with AJAX
- Add a post meta key and value only if it does not exist on the post
- Custom fields to save multiple values
- Function to change meta value in database for each post
- wp_handle_upload error “Specified file failed upload test” but still creates attachment?
- Which is best in the following scenario : post_meta vs custom table vs parent/child posts
- Saving custom image meta fields
- Get all meta keys assigned to a post type
- How to sort category by custom field value
- trim custom field text value and show (…)
- Query post order by post and desc not working
- Adding custom fields (post meta) before/during wp_insert_post()
- How to query posts with certain custom meta data, and output Post data
- ACF: How to get the full field name (meta_key) by a field key?
- post meta getting deleted on save
- How to add a new meta key and assign timestamp to posts
- Custom field not updating when value is empty
- meta_compare seems to be treating values as strings instead of integers as expected
- Limit the number of acf content when displaying in post loop [closed]
- Read / Watch / Listen times – meta
- How to add custom metadata text box dropdown to sidebar in Gutenberg editor for all post types
- How do I Implement Atomic Update of Post Metadata?
- How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
- Displaying multiple URLs as custom field values
- Show values of custom post meta on ‘Add new post’ page?
- Custom post meta field effect on the performance on the post
- get_posts in meta box dropdown not showing latest posts
- Limits, not all post are showen when querying for posts by view count
- Display Custom Meta Box Field Only If Value is Present
- Ordering posts by custom field named “date” in backend
- Stop sending email everytime I updates my post
- Best way to store everyday post views?
- pre_get_posts : only get posts by wp_usermeta value
- Custom meta is not being saved
- Looping inside block return
- change order of images attached to post
- Sortable Custom Columns not sorting correct
- how can I show name and value of Custom Fields together?
- Can I save post meta programatically without setting metaboxes?
- Meta box values are displayed on Custom Fields list. Is it possible to hide them?
- Gravity Forms Update post meta from template [closed]
- Best way to achieve multiple links in a post title
- How can I add/update post meta in a admin menu page?
- Show array of meta_value in Edit Post Coloum
- custom filed from post in the side bar
- wordpress multi user question
- How to change text color depending on the number value (Using javascript)
- Adding Custom Metadata to my Archive/Posts page
- How to improve my non-unique metadata MySQL entries?
- Custom Fields Not Showing (ACF not installed)
- SQL query to change the value of a Custom Field
- Custom meta POST request fired twice when updating a post in Gutenberg
- Add custom field information to source meta data
- How do I update custom field post meta in frontend post template?
- How to Find List of Available Custom Fields for Theme?
- Create Shortcode shows only posts with custom_field meta
- Create Meta boxes dynamically
- Custom Fields Not Working In Footer
- ACF plugin and field update
- Delete custom meta
- Search one custom field?
- Can’t save meta field value if the title not set
- Adding custom fields to images
- How to save dynamically generated value in post type?
- wordpress simple post multi rating with post_meta and user_meta
- how to show this part only in the single post page?
- Display meta data from a custom field within plugin Category Grid View Gallery
- Custom fields / meta box output
- If post custom meta data is not set, show normal state
- Colecting values from custom field checkboxes and displaying them in the post
- Build Array from Input Fields question
- Display a list of posts whose meta field values are equal to the ID of the post being viewed?
- custom field with links
- How can i put a custom field inside this php
- Check for custom field value in different post type than current one and do something
- How to add new Metadata options (Date, Author, etc.) for Posts?
- Can I count every article following extracted meta value?
- What is the best way to get a different post’s custom field/postmeta with js?