0

I have a post_meta field that seems to be displaying multiple values, yet they do not seem to be in an array. Using debug bar, this is what I see inside of the '_custom_post_type_onomies_relationship' meta field.

screenshot

I want to filter these id's to return titles instead. Because I am using Facet WP, I need the values to NOT be an array when done.

$item_array = get_post_meta( $post->id, '_custom_post_type_onomies_relationship' );
foreach ( $item_array as $item_id ) {
   $title = get_the_title( $item_id );
   $item_array[] = $title;
}

update_post_meta( $post->id, '_custom_post_type_onomies_relationship', $item_array );

This works just fine except that it is an array. What data type is that custom field before I filter it? How do I reproduce that data type?

Note: Before I filter it, the data type must not be an array because FacetWP can understand it.

2

Meta data keys do not have to be unique, you can have multiple rows of the same key, each containing singular values. If you use get_post_meta to fetch a key and don't set the 3rd argument to true, the function gets all the individual values and returns them as an array, which is not the same as getting a single value that is an array saved as a serialized string.

If you want to iterate over the returned array and save new values, you have to call update_post_meta for each item in the array, and set the 4th argument of the function, which is the old value of the particular row you'd like to update.

$item_array = get_post_meta( $post->id, '_custom_post_type_onomies_relationship' );
foreach ( $item_array as $item_id ) {
   $title = get_the_title( $item_id );
   update_post_meta( $post->id, '_custom_post_type_onomies_relationship', $title, $item_id );
}
  • What would be a good hook to try this on? save_post? – lime Mar 7 '15 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.