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]
- Hide username from blog post source code
- How to add/update post meta to use in query?
- Update an additional user meta field with a string
- Get post content before rendering
- Reverse Cross-Sells (WooCommerce)
- get_category_parents to array
- gettext localization is not working
- wp_insert_post inside save_post adds wrong metadata to inserted post
- Notice : Array to string conversion on array_intersect user meta
- Rename image filename using ‘media_handle_upload’
- Combine multiple queries, array_unique returns nothing
- Add another role to a user when they click a button?
- Get field in readable word
- Move Entry Meta Above Title in Archives (Genesis + Brunch Pro)
- How do I update a specific value within array in a products metadata?
- When working with a post, almost all wp_postmeta are deleted
- Update Post metafield of specific categories
- Make sticky post with FacetWP
- Optimize WP Meta Query for large amount of post meta?
- Post Meta statement?
- how to query posts using value in meta post array
- not able to access $_POST on backend profile update
- Plugin error “array_key_exists(): The first argument should be either a string or an integer” [closed]
- Stored meta from attachment, video length?
- Programatically delete several attributes in _product_attributes in post meta
- Meta keys won’t add using wp_insert_post
- Right way to display the_author_meta fields?
- rendering open graph meta tags in wordpress
- Why doesn’t wp_oembed_get() for the video post format not work?
- Using Self Hosted Video URL With Custom Fields
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- Updating Lat and Lng of posts automatically gives sporadic results
- Re-pointing images to cloud storage
- Custom Meta Fields that are Echo’d are removed on post update?
- PHP array to jQuery array ” Uncaught SyntaxError: Unexpected token < "
- Get meta_query value by user meta array
- Creating a custom register form
- How i can extract data
- make an array in wordpress cusmization api?
- Get post meta after insert post
- Data not displaying in text field
- Trouble with checked() for array of multiple checkboxes
- How do you sort the items in a custom taxonomy meta box?
- Display posts using post ID’s in an array
- How can i iterate through this shortcode array?
- How to add dynamic content in title and meta description in wordpress theme for homepage, post page, category, tag and pages
- undesrtanding get_post_meta function
- Display a single row from nested array of custom field data
- How to replace hard-coded list of custom taxonomy terms based on custom field query [closed]
- Efficient way of querying for a “fallback” post?
- Cant get unique_array() work on get_the_category() foreach loop
- Get a list of tags present in a paged front page
- Multiple postmeta values to the same post_id/meta_key combination?
- How to call get_post()?
- Store stripe info as user_meta
- How Can I extract Image urls from post meta