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) false
becomesarray (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 do I retrieve the slug of the current page?
- How to save an array with one metakey in postmeta?
- WordPress is stripping escape backslashes from JSON strings in post_meta
- Get meta_id along with meta_key and meta_value
- Calling a method from functions.php on a click of a button
- Save both current and new version of post meta
- get_post_meta / update_post_meta array
- update_post_meta not adding anything.(Nor add_post_meta)
- How metadata API works?
- Issue with foreach on duplicate meta_key’s
- How to sanitize post meta field value?
- List of posts by day of the week
- Create a Metabox that behaves Like a Taxonomy Box
- Views count with time limit per IP
- Is it possible to store visitors IPs in wp_postmeta table?
- order by multiple meta_keys?
- Change description on specific WooCommerce product status change
- Create shortcodes within foreach loop (using array)
- How to programmatically customise the Contact Form7 notification email prior to sending? [closed]
- Saving repeated option values when querying in Published, draft and all
- How to hide meta block(s) in certain post format
- Get post id within comments loop
- Get user_meta values for a user for an array of meta_keys?
- how to get a list of meta data fields assocaited with a custom post type
- Get array of metakey in all posts
- String taken from database table post_meta is showing HTML tags on Frontend
- Delete posts based on post meta data
- Login redirect. Check user meta and redirect accordingly
- Adding Author Box Meta Links with Co-Authors
- How do I edit the posted “by” and “on” in the post meta
- How to delete duplicate records in wp_postmeta database table?
- Conditional For get_the_author_meta
- get value from get_post_meta then reuse it in another get_post_meta
- I Can’t get the post_tag name from term_id using wp_set_object_terms
- Creating a custom post type upon registration for a specific user role
- Using zip code to display custom data in Admin Order Details
- ACF – Get ID of relationship field in flexible content
- why is my postmeta table is so heavy
- Using a comma instead of a pipe to separate metadata
- Add Array record to Meta Post
- Meta keys won’t add using wp_insert_post
- Cant get unique_array() work on get_the_category() foreach loop
- How Can I extract Image urls from post meta
- Plugin Shortcode value in post
- Twenty Twelve entry meta data – change and move
- Unserializeing multiple column values that are stored in one database results variable
- Meta-Box to add multiple items one at a time and on publish save all
- Values inside a custom field to determine which category posts to display
- wordpress simple post multi rating with post_meta and user_meta
- Sorting meta box values by start- and enddate and all dates in between
- Use an id on a button to update_post_meta in post
- get_post_meta image width for lazy load
- get_permalink returning first letter
- Custom Fields help
- WordPress User Frontend Editing Custom Fields
- Parse error thrown by get_post_meta [closed]