I normalize the output of these functions using this logic:
- Cast the value to an array. If it is not an array already, it will be converted to an array with one element, which is the value it was, e.g.
(array) falsebecomesarray (false). - Use
array_filter, which, when not passed a callable, simply filters falsy values from the array.
Here is this logic in action:
foreach (array_filter((array) get_post_meta($id, $meta_key)) as $value) {
// ...
}
Warning: This will strip all falsy values, e.g. if your meta value is zero or an empty string, it will be not be iterated through.
Related Posts:
- Array of user ids to list of user names
- How to extract data from a post meta serialized array?
- Content hooks vs User hooks
- How can merge two arrays values in one array and save in database
- How to update single value in multi dimensional Post Meta?
- How to store post meta in an array?
- Compare two meta key values against each other inside the get_posts array?
- Job of meta_key meta_value fields in database tables
- Unset field from an array not working as expected
- Creating user status mode in WordPress
- Like & Dislike functionality using JavaScript
- Post meta as array looks like string instead of array
- Time & Date on Post – Time Ago Custom Function
- Output meta into arrays
- Can A Post Meta Field Store multiple values that are not in an array?
- How to use two meta_compare in an array?
- If Array Values Match Another Array’s Values, Then
- Remove empty terms from array, sort alphabetically, update back to repeating field
- WordPress loop by meta key that is an array? and how loop multiple arrays
- Comapare get_user_meta value
- How can get all users by current user meta?
- Order a WP_Query by meta value where the value is an array
- Displaying values from related field – if empty shows current post type’s link and title
- Update value of a associative array with update_post_meta
- Retrieve user_meta and copy to post_meta
- get Insert id for meta field
- help to decipher wp metadata
- update_post_meta() updating nested array in Multidimensional array with empty sub-array
- Check For get_post_meta on Author Archive Page
- How can I update this array built from post meta data?
- What format is this and how to get value “lal” and “pila” from it
- How to add and subtract user meta values after post meta update
- How to get posts by meta value as multi-dimensional array?
- WP post meta – for loop inside for loop
- Store custom field’s multiple values in one user meta key
- Remove item from post_meta array via AJAX
- show user based on user_meta
- Multiple meta key and value search in the query
- Adding user meta to post meta wp_insert_post() not working
- get_users when from meta key that has serialized values
- How can get all users by current user meta (array)?
- get_post_meta not work in php foreach
- Fetching array of postmeta with $wpdb and in_array conditional
- wp_postmeta store multiple values in one key [closed]
- How can I get the post ID from a WP_Query loop?
- Can I count the number of users matching a value in a multiple value key?
- How to update/delete array in post meta value?
- Update Multiple Post Meta for the Same Post In One call?
- adding a URL to a post meta
- Using a javascript file to access a get posts array
- Automatically adding meta data to posts or multiple query help
- Multiple User-Meta Values in One User-Meta Key
- How to add meta data to WordPress posts?
- How add post_meta from user_meta?
- How can I get my iFrame to work with url from post_meta?
- Storing Array from returned database query and using the array in a new query
- How to use an associative array in post__in with WP_Query?
- How can I filter posts when the meta_value is a serialize object?
- Get User Post if Private
- Adding number to date not working [closed]
- Unknown characters added to meta data values
- Compare old meta with new post meta
- get_post_meta($post->ID) returns empty string when in preview mode of custom post type
- Read post meta values, only if posts are public
- Can’t access PHP array inside script localization from javascript
- Mass removing CSS from 1,700 post [closed]
- Filtering a function’ output for a new continued function
- Help with Post Meta
- last_name + first_name orderby with meta_query [solved]
- I’m unable to call img path using single quotes in an array?
- count post meta values
- Correct meta of Writing Meta Tags
- Define category ID using get_post_meta
- How to merge the array values in foreach?
- Order category posts by last word of custom field
- in_array not working on dev server but works on localhost
- How to display MySQL table data which is stored as an array?
- update_post_meta not working with transition_comment_status
- Fix common misspellings/errors based on array of replacements
- Where do I implement this display of User Meta Data, and how to put it in a table?
- Attachment metadata has value of ‘1’
- Unable to update the meta boxes with multiple fields
- How do I update a specific value within array in a products metadata?
- Optimize WP Meta Query for large amount of post meta?
- Restore deleted _thumbnail_id in postmeta table
- Post from front end form to post_meta
- Reformat data within a nested array
- Hide the_meta if no value
- php wp_insert data on front using a form
- Updating post_meta when updating a setting with the Settings API
- How to show specific meta keys of all posts in admin panel?
- Media library orphans
- How to make certain content of the post noindex and no follow. not entire post?
- How to set if meta_value is lower < than other meta_value in a get_posts array?
- Populate checkboxes from array
- Auto save title as custom meta field value
- Combine two foreach Arrays into one table row
- How to show wordpress post in the site based on custom field value?
- Why isn’t my embed_video WYSIWYG field updating when using update_post_meta?
- Problem with custom user fields default value and retrieval